Get the parent rows of a row by foreign key
@param \pq\Gateway\Row $foreign
@param string $ref
@return mixed
968220
function by(Row $foreign, $ref = null) {
		// select * from $this where $this->$referencedColumn = $me->$foreignColumn
		
		if (!($rel = $foreign->getTable()->getRelation($this->getName(), $ref))) {
			return $this->onResult(null);
		}
		
		$where = array();
		foreach ($rel as $key => $ref) {
			$where["$ref="] = $foreign->$key;
		}
		return $this->find($where);
	}
743349
public function getForeignKey(): ForeignKey
    {
        if ($this->foreignKey === null) {
            $pk = $this->getPrimaryKey();
            $prefix = $this->getEntityName();
            $this->foreignKey = new class($pk, $prefix) extends ForeignKey
            {
                /**
                 *  constructor.
                 * @param PrimaryKey $primaryKey
                 * @param string $prefix
                 */
                public function __construct(PrimaryKey $primaryKey, string $prefix)
                {
                    $columns = [];
                    foreach ($primaryKey->columns() as $column) {
                        $columns[$column] = $prefix . '_' . $column;
                    }
                    parent::__construct($columns);
                }
            };
        }

        return $this->foreignKey;
    }
1156831
protected function belongsTo($referenceModel, $foreignKey = null)
    {
        return (new Relations\BelongsTo(
            new Relations\Maps\Inverse($this, $referenceModel, $foreignKey)
        ))->getResult();
    }
159101
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		if ($parent->getQuery()->from == $query->getQuery()->from) {
			return $this->getRelationQueryForSelfRelation($query, $parent, $columns);
		}

		$query->select($columns);

		$otherKey = $this->wrap($query->getModel()->getTable().'.'.$this->otherKey);

		return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
	}
1058161
public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
    {
        // If no relationship name was passed, we will pull backtraces to get the
        // name of the calling function. We will use that function name as the
        // title of this relation since that is a great convention to apply.
        if (is_null($relation)) {
            $relation = $this->getBelongsToManyCaller();
        }

        $foreignKey = $foreignKey ?: $this->getCmsReferenceKeyForRelation($relation);
        $otherKey   = $otherKey   ?: $this->getCmsReferenceKeyForRelation($relation, true);

        if (is_null($table)) {
            $table = $this->getCmsJoiningTable();
        }

        $fieldId = $this->getCmsReferenceFieldId($relation);
        if (empty($fieldId)) {
            throw new InvalidArgumentException("No 'field' id configured for relation/reference: '{$relation}'!");
        }

        // originally, it was possible to just get the parent's BelongsToMany instance, but
        // to avoid problems when saving on the relationship, we need a specialized extension
        // of the relation class for the CMS.
        //$belongsToMany = parent::belongsToMany($related, $table, $foreignKey, $otherKey);

        /** @var Model $instance */
        $instance = new $related;

        $query = $instance->newQuery();

        $belongsToMany = new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation, $fieldId);

        // add constraints
        $belongsToMany->wherePivot(config('pxlcms.relations.references.keys.field', 'from_field_id'), $fieldId);

        return $belongsToMany;
    }
1259277
def get_parent(self, refobj):
        
        c = cmds.listConnections("%s.parent" % refobj, source=False)
        return c[0] if c else None
1279723
public function belongs($name, $foreignKeyOrCallback = null, $callback = null, $storeKey = null)
    {
        $foreignKey = null;
        if (func_num_args() < 4 && is_callable($foreignKeyOrCallback)) {
            $storeKey = $callback;
            $callback = $foreignKeyOrCallback;
        } else {
            $foreignKey = $foreignKeyOrCallback;
        }
        $foreignKey = $foreignKey ? : $this->rows
            ->getTable()
            ->getStructure()
            ->foreignKey($name, $this->getRows()->getTable()->getTableName());
        $relations = $this->getRows()->loadBelongRelations($name, $foreignKey, $callback, $storeKey);
        $this->loadedRelations[] = [
            $storeKey ? : $name, 'belongs', $foreignKey
        ];
        return $relations->find($this->$foreignKey);
    }
246046
public function getMappedLocalColumn($foreign)
    {
        $index = array_search($foreign, $this->foreignColumns);
        return $this->localColumns[$index];
    }
518547
static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
	{
		$ar = new $parentclass();
		$ar->hasMany($foreignRef, $foreignKey, $foreignClass);
	}
1220912
private function queueRelated(
        ContextualCommandInterface $parentCommand
    ): ContextualCommandInterface {
        /*
         * Only queue parent relations when parent wasn't saved already, attention, this might
         * cause an issues with very deep recursive structures (and it's expected).
         */
        $innerCommand = $this->instance->queueStore(
            !$this->instance->isLoaded()
        );

        if (!$this->isSynced($this->parent, $this->instance)) {
            //Syncing FKs before primary command been executed
            $innerCommand->onExecute(function ($innerCommand) use ($parentCommand) {
                $parentCommand->addContext(
                    $this->key(Record::INNER_KEY),
                    $this->lookupKey(Record::OUTER_KEY, $this->parent, $innerCommand)
                );

                //Morph key value
                $parentCommand->addContext(
                    $this->key(Record::MORPH_KEY),
                    $this->orm->define(get_class($this->instance), ORMInterface::R_ROLE_NAME)
                );
            });
        }

        return $innerCommand;
    }
233610
public function getAncestor(ConnectionInterface $con = null)
    {
        if ($this->aAncestor === null && ($this->ancestor_id !== null)) {
            $this->aAncestor = ChildSkillQuery::create()->findPk($this->ancestor_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aAncestor->addLineagesRelatedByAncestorId($this);
             */
        }

        return $this->aAncestor;
    }
1341173
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) {
            return $this->getRelationExistenceQueryForThroughSelfRelation($query, $parentQuery, $columns);
        }

        $this->performJoin($query);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedLocalKeyName(), '=', $this->getQualifiedFirstKeyName()
        );
    }
1023285
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName()
        );
    }
885031
public function getChild ($relationIdentifier, $id, $childId)
		{
			$relation = $this->getRelation($relationIdentifier);

			$query = $this->database->createQuery()->whereEqual(
				$relation->getRelatedTable() . '.' . $this->database->primaryKey($relation->getRelatedTable()),
				$childId
			);

			return $this->getChildren($relationIdentifier, $id, $query)->fetchRow();
		}
83491
public function getOwner($relatedClass, $foreignKey = '')
    {
        if (!$foreignKey) {
            $column = $relatedClass::getTable();
        } else {
            $column = $foreignKey;
        }
        return static::wrap($this->fetchAs($relatedClass)->$column, $relatedClass);
    }
1226458
public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
    {
        $sql = '';
        if (strlen($foreignKey->getName())) {
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
        }
        $sql .= 'FOREIGN KEY (';

        if (count($foreignKey->getLocalColumns()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'local' required.");
        }
        if (count($foreignKey->getForeignColumns()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'foreign' required.");
        }
        if (strlen($foreignKey->getForeignTableName()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
        }

        return $sql . implode(', ', $foreignKey->getQuotedLocalColumns($this))
            . ') REFERENCES '
            . $foreignKey->getQuotedForeignTableName($this) . ' ('
            . implode(', ', $foreignKey->getQuotedForeignColumns($this)) . ')';
    }
182674
protected function getForeignKey()
	{
		if($this->foreignKey === null)
		{
			$this->foreignKey = $this->parent->getForeignKey();
		}

		return $this->foreignKey;
	}