Initialize many to many relations.

@return \MwbExporter\Model\Table
559592
public function initManyToManyRelations()
    {
        if ($this->isManyToMany()) {
            $fk1 = $this->foreignKeys[0];
            $fk2 = $this->foreignKeys[1];
            $this->injectManyToMany($fk1, $fk2);
            $this->injectManyToMany($fk2, $fk1);
        }

        return $this;
    }
239844
public function initRelation()
    {
        // Turn models into nodes
        $this->start = $this->asNode($this->parent);
        $this->hyper = $this->asNode($this->related);
        $this->end   = $this->asNode($this->morph);

        // Not a unique relationship since it involves multiple models.
        $unique = false;

        // Setup left and right edges
        $this->left  = new EdgeOut($this->query, $this->parent, $this->related, $this->type, $this->attributes, $unique);
        $this->right = new EdgeOut($this->query, $this->related, $this->morph, $this->morphType, $this->attributes, $unique);
        // Set the morph type to the relationship so that we know who we're talking to.
        $this->right->morph_type = get_class($this->morph);
    }
756368
protected function _setJoins()
    {
        $joins = $this->_sql->getJoins();
        foreach ($joins as $join) {
            $this->_statement .= $this->_createJoinStatement($join);
        }
        return $this;
    }
652445
protected function getForeignKeyRelations() {
    if ($this->fkRelations == null) {
      $this->fkRelations = [];
      $relationDescs = $this->getRelations();
      foreach($relationDescs as $relationDesc) {
        if ($relationDesc instanceof RDBManyToOneRelationDescription) {
          $this->fkRelations[] = $relationDesc;
        }
      }
    }
    return $this->fkRelations;
  }
92127
public function requireTable()
    {
        // Only build the table if we've actually got fields
        $schema = static::getSchema();
        $table = $schema->tableName(static::class);
        $fields = $schema->databaseFields(static::class, false);
        $indexes = $schema->databaseIndexes(static::class, false);
        $extensions = self::database_extensions(static::class);

        if (empty($table)) {
            throw new LogicException(
                "Class " . static::class . " not loaded by manifest, or no database table configured"
            );
        }

        if ($fields) {
            $hasAutoIncPK = get_parent_class($this) === self::class;
            DB::require_table(
                $table,
                $fields,
                $indexes,
                $hasAutoIncPK,
                $this->config()->get('create_table_options'),
                $extensions
            );
        } else {
            DB::dont_require_table($table);
        }

        // Build any child tables for many_many items
        if ($manyMany = $this->uninherited('many_many')) {
            $extras = $this->uninherited('many_many_extraFields');
            foreach ($manyMany as $component => $spec) {
                // Get many_many spec
                $manyManyComponent = $schema->manyManyComponent(static::class, $component);
                $parentField = $manyManyComponent['parentField'];
                $childField = $manyManyComponent['childField'];
                $tableOrClass = $manyManyComponent['join'];

                // Skip if backed by actual class
                if (class_exists($tableOrClass)) {
                    continue;
                }

                // Build fields
                $manymanyFields = array(
                    $parentField => "Int",
                    $childField => "Int",
                );
                if (isset($extras[$component])) {
                    $manymanyFields = array_merge($manymanyFields, $extras[$component]);
                }

                // Build index list
                $manymanyIndexes = [
                    $parentField => [
                        'type' => 'index',
                        'name' => $parentField,
                        'columns' => [$parentField],
                    ],
                    $childField => [
                        'type' => 'index',
                        'name' => $childField,
                        'columns' => [$childField],
                    ],
                ];
                DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, $extensions);
            }
        }

        // Let any extentions make their own database fields
        $this->extend('augmentDatabase', $dummy);
    }
11244
public function initialize()
    {
        $this->_connection->table(self::TABLE)
            ->createIfNotExists(array(
                'name' => new \Pheasant\Types\StringType(255, 'notnull primary'),
                'id' => new \Pheasant\Types\IntegerType(null, 'notnull unsigned'),
                ));

        return $this;
    }
413774
public function getRelations()
    {
        if (!$this->relationsBuilt) {
            $this->buildRelations();
            $this->relationsBuilt = true;
        }

        return $this->relations;
    }
835203
protected function prepareProductRelationAttributes()
    {

        // initialize and return the entity
        return $this->initializeEntity(
            array(
                MemberNames::PARENT_ID => $this->parentId,
                MemberNames::CHILD_ID  => $this->childId
            )
        );
    }
455350
protected function createRelations()
    {
        foreach($this->schema['relations'] as $table => $many) {
            $this->database->query('DROP TABLE IF EXISTS `'.$this->schema['name'].'_'.$table.'`');

            $this->database->query('CREATE TABLE `'.$this->schema['name'].'_'.$table.'` (
              `'.$this->schema['name'].'_'.$table.'_'.$this->schema['name'].'` int(10) unsigned NOT NULL,
              `'.$this->schema['name'].'_'.$table.'_'.$table.'` int(10) unsigned NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8');

            $this->database->query('ALTER TABLE `'
                . $this->schema['name'] . '_' . $table . '`'
                 . 'ADD PRIMARY KEY (`'
                . $this->schema['name'] . '_' . $table . '_' . $this->schema['name'] . '`,`'
                . $this->schema['name'] . '_' . $table . '_' . $table . '`), '
                . 'ADD KEY `' . $table . '_id_idx` (`'
                . $this->schema['name'] . '_' . $table . '_' . $table . '`)');
        }

        return $this;
    }
23887
public function tagsRelationTable() {
        $model_name = $this->owner->formName();
        if($model_name > 'Tag') {
            $relationship_table = 'Tag_' . $model_name;
        } else {
            $relationship_table = $model_name . '_Tag';
        }
        return $relationship_table;
    }
686597
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model)
        {
            // In the case of fetching nested relations, we will get an array
            // with the first key being the model we need, and the other being
            // the related model so we'll just take the first model out of the array.
            if (is_array($model)) $model = reset($model);

            $model->setRelation($relation, null);
        }

        return $models;
    }
5620
public function doFinalInitialization()
    {
        // add the referrers for the foreign keys
        $this->setupTableReferrers();

        // execute database behaviors
        foreach ($this->getBehaviors() as $behavior) {
            $behavior->modifyDatabase();
        }

        // execute table behaviors (may add new tables and new behaviors)
        while ($behavior = $this->getNextTableBehavior()) {
            $behavior->getTableModifier()->modifyTable();
            $behavior->setTableModified(true);
        }

        // do naming and heavy indexing
        foreach ($this->tables as $table) {
            $table->doFinalInitialization();
            // setup referrers again, since final initialization may have added columns
            $table->setupReferrers(true);
        }
    }
789168
protected function init() {
        $this->data['crud'] = 'd';
        $this->data['edulevel'] = self::LEVEL_OTHER;
        $this->data['objecttable'] = template::TABLE;;
    }
178503
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, []);
        }
        return $models;
    }
632958
public function reset() {
		$this->tables  = [];
		$this->columns = [];
		$this->where   = [];
		$this->offset  = null;
		$this->limit   = null;
		$this->orderBy = [];
		$this->groupBy = [];
		$this->joins   = [];
		$this->params  = [];

		return $this;
	}
297597
protected function getRelations()
    {
        $relations = [];
        $rawRelations = $this->getRawRelations();
        foreach ($rawRelations as $rawRelation) {
            $relations[] = $this->getRealtion($rawRelation->foreignTable, $rawRelation->foreignKey, $rawRelation->localKey, $rawRelation->selfReferences);
        }

        return $relations;
    }
302384
private function buildRelationships() {
        /** @var Relationship[] $relationships */
        $relationships = array();
        $dbCompareTable = new Table($this->getConfiguration()->getConnection(), $this->getTableName());
        foreach($dbCompareTable->getConstraints() as $constraint) {
            $relationship = new Relationship(
                $this->getConfiguration(),
                $this->getTableName(),
                implode(', ', $constraint->getLocalColumns()),
                $constraint->getRemoteTable(),
                implode(', ', $constraint->getRemoteColumns()),
                'belongsTo',
                'ON DELETE ' . $constraint->getDeleteAction() . ' ON UPDATE ' . $constraint->getUpdateAction()
            );
            $this->getAbstractClass()->addUse('Phalcon\Mvc\Model\Relation');
            $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel());
            $relationships[] = $relationship;
        }

        //Get One to Many relationships

        $database = $this->getCurrentDatabaseName();
        $sql = 'SELECT DISTINCT(TABLE_NAME) as tableName FROM information_schema.`KEY_COLUMN_USAGE` WHERE REFERENCED_TABLE_NAME="' . $this->getTableName() . '" AND REFERENCED_TABLE_SCHEMA="' . $database . '"';
        /** @var ResultSet $results */
        $foreignKeys = $this->getConfiguration()->getConnection()->query($sql);
        while($foreignKey = $foreignKeys->fetch()) {
            $foreignKey = (object)$foreignKey;
            $tableName = $foreignKey->tableName;

            $dbCompareTable = new Table($this->getConfiguration()->getConnection(), $tableName);
            foreach($dbCompareTable->getConstraints() as $constraint) {
                if($constraint->getRemoteTable() == $this->getTableName()) {
                    if(implode(', ', $constraint->getRemoteColumns())==='venueId`, `userID') {
                        exit;
                    }
                    $relationship = new Relationship(
                        $this->getConfiguration(),
                        $this->getTableName(),
                        implode(', ', $constraint->getRemoteColumns()),
                        $tableName,
                        implode(', ', $constraint->getLocalColumns()),
                        'hasMany',
                        'ON DELETE ' . $constraint->getDeleteAction() . ' ON UPDATE ' . $constraint->getUpdateAction()
                    );
                    $this->getAbstractClass()->addUse('Phalcon\Mvc\Model\Relation');
                    $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel());
                    $this->getAbstractClass()->addUse('Phalcon\Mvc\Model\Relation');
                    $this->getAbstractClass()->addUse('Phalcon\Mvc\Model\Resultset\Simple as ResultSet');
                    $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel());
                    $relationships[] = $relationship;
                }
            }

        }
        foreach($relationships as $relationship) {
            $relationship->setRelationships($relationships);
        }
        $this->setRelationships($relationships);
    }