/** * Create a new belongs to relationship instance. * * @param \Analogue\ORM\System\Mapper $mapper * @param Mappable $parent * @param string $foreignKey * @param string $otherKey * @param string $relation */ public function __construct(Mapper $mapper, Mappable $parent, $foreignKey, $otherKey, $relation) { $this->otherKey = $otherKey; $this->relation = $relation; $this->foreignKey = $foreignKey; parent::__construct($mapper, $parent); }
/** * Create a new has many relationship instance. * * @param \Analogue\ORM\System\Query $query * @param Mappable $parent * @param string $firstKey * @param string $secondKey * @return void */ public function __construct(Mapper $mapper, $farParent, $parentMap, $firstKey, $secondKey) { $this->firstKey = $firstKey; $this->secondKey = $secondKey; $this->farParent = $farParent; $this->farParentMap = $this->relatedMapper->getManager()->mapper($farParent)->getEntityMap(); $parentInstance = $this->relatedMapper->getManager()->mapper($parentMap->getClass())->newInstance(); parent::__construct($mapper, $parentInstance); }
/** * Add the constraints for a relationship count query. * * @param \Analogue\ORM\Query $query * @param \Analogue\ORM\Query $parent * @return \Analogue\ORM\Query */ public function getRelationCountQuery(Query $query, Query $parent) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationCountQueryForSelfJoin($query, $parent); } $this->setJoin($query); return parent::getRelationCountQuery($query, $parent); }
/** * Get the relation instance for the given relation name. * * @param string $relation * @return \Analogue\ORM\Relationships\Relationship */ public function getRelation($relation) { // We want to run a relationship query without any constrains so that we will // not have to remove these where clauses manually which gets really hacky // and is error prone while we remove the developer's own where clauses. $query = Relationship::noConstraints(function () use($relation) { return $this->entityMap->{$relation}($this->getEntityInstance()); }); $nested = $this->nestedRelations($relation); // If there are nested relationships set on the query, we will put those onto // the query instances so that they can be handled after this relationship // is loaded. In this way they will all trickle down as they are loaded. if (count($nested) > 0) { $query->getQuery()->with($nested); } return $query; }
/** * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any. * * @param [type] $parentKey [description] * @param [type] $relation [description] * @param [type] $result [description] * @param Relationship $relationship [description] * @return [type] [description] */ protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship) { $pivotColumns = $relationship->getPivotAttributes(); if (!array_key_exists($relation, $this->pivotAttributes)) { $this->pivotAttributes[$relation] = $pivotColumns; } $hash = $this->getEntityHash($result); if (count($pivotColumns) > 0) { $pivotAttributes = []; foreach ($pivotColumns as $column) { $pivotAttributes[$column] = $result->getEntityAttribute('pivot')->getEntityAttribute($column); } $cachedRelationship = new CachedRelationship($hash, $pivotAttributes); } else { $cachedRelationship = new CachedRelationship($hash); } return $cachedRelationship; }
/** * Create a new has many relationship instance. * * @param \Analogue\ORM\System\Query $query * @param Mappable $parent * @param string $foreignKey * @param string $localKey * @return void */ public function __construct(Mapper $mapper, $parentEntity, $foreignKey, $localKey) { $this->localKey = $localKey; $this->foreignKey = $foreignKey; parent::__construct($mapper, $parentEntity); }