Constrains the relationship content to the applicable rows on the related model

@return  object
@since   2.0.0
175135
public function constrain()
	{
		return $this->related->whereEquals($this->relatedKey, $this->model->{$this->localKey});
	}
681810
public function addConstraints()
    {
        parent::addConstraints();

        if (static::$constraints) {
            if (is_array($this->foreignKeys[0])) {
                $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);

                $this->query->where($column, '=', $this->farParent->getMorphClass());
            }
        }
    }
578674
public function addConstraints()
    {
        if (static::$constraints)
        {
            // Get the parent node's placeholder.
            $parentNode = $this->query->getQuery()->modelAsNode($this->parent->getTable());
            // Tell the query that we need the morph model and the relationship represented by CypherGrammar
            // statically with 'r'.
            $this->query->select($this->relation, 'r');
            // Add morph mutation that will tell the parser about the property name on the Relationship that is holding
            // the class name of our morph model so that they can instantiate the correct one, and pass the relation
            // name as an indicator of the Node that has our morph attributes in the query.
            $this->query->addMorphMutation($this->relation);
            // Set the parent node's placeholder as the RETURN key.
            $this->query->getQuery()->from = array($parentNode);
            // Build the MATCH ()<-[]-() Cypher clause.
            $this->query->matchMorphOut($this->parent, $this->relation, $this->foreignKey, $this->parent->{$this->foreignKey});
            // Add WHERE clause over the parent node's matching key = value.
            $this->query->where($this->foreignKey, '=', $this->parent->{$this->foreignKey});
        }
    }
549345
protected function getOtherKey()
    {
        if ($this->otherKey) {
            return $this->otherKey;
        }

        if (is_callable([$this->form->model(), $this->column]) &&
            ($relation = $this->form->model()->{$this->column}()) instanceof BelongsToMany
        ) {
            /* @var BelongsToMany $relation */
            $fullKey = $relation->getQualifiedRelatedPivotKeyName();

            return $this->otherKey = substr($fullKey, strpos($fullKey, '.') + 1);
        }

        throw new \Exception('Column of this field must be a `BelongsToMany` relation.');
    }
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);
    }
709486
private function buildRelationshipsList() {
        $content = '		return array(' . "\n";
        $added = false;
        foreach($this->getRelationships() as $relationship) {
            if(!$relationship->isPlural()) {
                $added = true;
                $content .= '			\'' . $relationship->getAlias() . '\',' . "\n";
            }
        }
        if($added) {
            $content = substr($content, 0, -2) . "\n"; //Remove trailing comma
        }
        $content .= '		);';
        $method = new Object\Method();
        $method->setAccess('public');
        $method->setDescription('Returns a list of all parent relationships, these will all return a Base Model instance');
        $method->setName('getParentRelationships');
        $method->setReturnType('string[] values are the alias of the relationship');
        $method->setContent($content);
        $this->getAbstractClass()->addMethod($method);

        $content = '		return array(' . "\n";
        $added = false;
        foreach($this->getRelationships() as $relationship) {
            if($relationship->isPlural()) {
                $added = true;
                $content .= '			\'' . $relationship->getAlias() . '\',' . "\n";
            }
        }
        if($added) {
            $content = substr($content, 0, -2) . "\n"; //Remove trailing comma
        }
        $content .= '		);';
        $method = new Object\Method();
        $method->setAccess('public');
        $method->setDescription('Returns a list of all children relationships, these will all return a ResultSet');
        $method->setName('getChildrenRelationships');
        $method->setReturnType('string[] values are the alias of the relationship');
        $method->setContent($content);
        $this->getAbstractClass()->addMethod($method);

    }
313540
public function addEagerConstraints(array $models)
    {
        if (is_array($this->ownerKey)) { //Check for multi-columns relationship
            $keys = [];

            foreach ($this->ownerKey as $key) {
                $keys[] = $this->related->getTable().'.'.$key;
            }

            $this->query->whereIn($keys, $this->getEagerModelKeys($models));
        } else {
            // We'll grab the primary key name of the related models since it could be set to
            // a non-standard name and not "id". We will then construct the constraint for
            // our eagerly loading query so it returns the proper models from execution.
            $key = $this->related->getTable().'.'.$this->ownerKey;
            $this->query->whereIn($key, $this->getEagerModelKeys($models));
        }
    }
859790
public function addConstraints()
    {
        $this->query->where($this->foreignKey, '=', $this->parent->{$this->localKey});
    }
267643
public function permissions()
    {
        $permissionModel = config('defender.permission_model');
        $permissionRoleTable = config('defender.permission_role_table');
        $roleKey = config('defender.role_key');
        $permissionKey = config('defender.permission_key');

        return $this->belongsToMany($permissionModel, $permissionRoleTable, $roleKey, $permissionKey)->withPivot('value', 'expires');
    }
380184
public function addConstraints()
    {
        if (static::$constraints) {
            $table = $this->related->getTable();

            $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->{$this->foreignKey});
        }
    }
1081228
public function dissociate()
    {
        $this->child->setAttribute($this->foreignKey, null);

        return $this->child->setRelation($this->relationName, null);
    }
551746
public function get()
    {
        // Perform before hook
        $this->beforeGet();

        // Set basic query
        $this->db()->table($this->table)->select($this->columns)->limit($this->limit)->offset($this->offset)->orderBy($this->orderBy);

        // Resolve relationships
        $this->hasOne()->belongsToMany();

        // Get results
        $this->records = $this->db()->get();

        // Add has many relationships
        $this->hasMany();

        // Format relationships
        $this->formatBelongsToMany()->formatHasOne();

        // Perform after hook
        $this->afterGet();

        // Return query results
        return $this->records;
    }
876018
public function onRelationManageRemove()
    {
        $this->beforeAjax();

        $recordId = post('record_id');
        $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey() : null;
        $relatedModel = $this->relationModel;

        /*
         * Remove
         */
        if ($this->viewMode == 'multi') {

            $checkedIds = $recordId ? [$recordId] : post('checked');

            if (is_array($checkedIds)) {
                $foreignKeyName = $relatedModel->getKeyName();

                $models = $relatedModel->whereIn($foreignKeyName, $checkedIds)->get();
                foreach ($models as $model) {
                    $this->relationObject->remove($model, $sessionKey);
                }
            }
        }
        /*
         * Unlink
         */
        elseif ($this->viewMode == 'single') {
            if ($this->relationType == 'belongsTo') {
                $this->relationObject->dissociate();
                $this->relationObject->getParent()->save();
            }
            elseif ($this->relationType == 'hasOne' || $this->relationType == 'morphOne') {
                if ($obj = $relatedModel->find($recordId)) {
                    $this->relationObject->remove($obj, $sessionKey);
                }
                elseif ($this->viewModel->exists) {
                    $this->relationObject->remove($this->viewModel, $sessionKey);
                }
            }

            $this->viewWidget->setFormValues([]);
        }

        return $this->relationRefresh();
    }
961957
public function getRelationDefinition()
    {
        $relations = $this->getOption('relations');
        if (!is_array($relations)) {
            return $this->relations();
        }

        return $relations + $this->relations();
    }
1372966
public function addRules()
    {
        if ($this->isMultiple) {
            $this->property->relatedPropertiesModel->addRule($this->property->code, 'safe');
        } else {
            $this->property->relatedPropertiesModel->addRule($this->property->code, 'integer');
        }

        if ($this->property->isRequired) {
            $this->property->relatedPropertiesModel->addRule($this->property->code, 'required');
        }

        return $this;
    }
451444
public function getRelationships()
    {
        if (empty(static::$relationHooks)) {
            $hooks = [];

            $rels = $this->getRelationshipsFromMethods(true);

            $this->getRelationshipsUnknownPolyMorph($rels, $hooks);

            $this->getRelationshipsKnownPolyMorph($rels, $hooks);

            $this->getRelationshipsHasOne($rels, $hooks);

            $this->getRelationshipsHasMany($rels, $hooks);

            static::$relationHooks = $hooks;
        }

        return static::$relationHooks;
    }
888077
public function addConstraints()
    {
        if (static::$constraints) {
            $foreignKey = $this->getForeignKeyName();
            $parentKeyValue = $this->getParentKey();

            //If the foreign key is an array (multi-column relationship), we adjust the query.
            if (is_array($this->foreignKey)) {
                foreach ($this->foreignKey as $index => $key) {
                    list(, $key) = explode('.', $key);
                    $fullKey = $this->getRelated()
                            ->getTable().'.'.$key;
                    $this->query->where($fullKey, '=', $parentKeyValue[$index]);
                    $this->query->whereNotNull($fullKey);
                }
            } else {
                $fullKey = $this->getRelated()
                        ->getTable().'.'.$foreignKey;
                $this->query->where($fullKey, '=', $parentKeyValue);
                $this->query->whereNotNull($fullKey);
            }
        }
    }