@@ -6,49 +6,49 @@ |
||
| 6 | 6 | |
| 7 | 7 | class MorphMany extends MorphOneOrMany |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Get the results of the relationship. |
|
| 11 | - * |
|
| 12 | - * @param $relation |
|
| 13 | - * @return mixed |
|
| 14 | - */ |
|
| 15 | - public function getResults($relation) |
|
| 16 | - { |
|
| 17 | - $results = $this->query->get(); |
|
| 18 | - |
|
| 19 | - $this->cacheRelation($results, $relation); |
|
| 20 | - |
|
| 21 | - return $results; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Initialize the relation on a set of models. |
|
| 26 | - * |
|
| 27 | - * @param array $entities |
|
| 28 | - * @param string $relation |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function initRelation(array $entities, $relation) |
|
| 32 | - { |
|
| 33 | - foreach ($entities as $entity) { |
|
| 34 | - $entity = $this->factory->make($entity); |
|
| 35 | - |
|
| 36 | - $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - return $entities; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Match the eagerly loaded results to their parents. |
|
| 44 | - * |
|
| 45 | - * @param array $entities |
|
| 46 | - * @param EntityCollection $results |
|
| 47 | - * @param string $relation |
|
| 48 | - * @return array |
|
| 49 | - */ |
|
| 50 | - public function match(array $entities, EntityCollection $results, $relation) |
|
| 51 | - { |
|
| 52 | - return $this->matchMany($entities, $results, $relation); |
|
| 53 | - } |
|
| 9 | + /** |
|
| 10 | + * Get the results of the relationship. |
|
| 11 | + * |
|
| 12 | + * @param $relation |
|
| 13 | + * @return mixed |
|
| 14 | + */ |
|
| 15 | + public function getResults($relation) |
|
| 16 | + { |
|
| 17 | + $results = $this->query->get(); |
|
| 18 | + |
|
| 19 | + $this->cacheRelation($results, $relation); |
|
| 20 | + |
|
| 21 | + return $results; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Initialize the relation on a set of models. |
|
| 26 | + * |
|
| 27 | + * @param array $entities |
|
| 28 | + * @param string $relation |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function initRelation(array $entities, $relation) |
|
| 32 | + { |
|
| 33 | + foreach ($entities as $entity) { |
|
| 34 | + $entity = $this->factory->make($entity); |
|
| 35 | + |
|
| 36 | + $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + return $entities; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Match the eagerly loaded results to their parents. |
|
| 44 | + * |
|
| 45 | + * @param array $entities |
|
| 46 | + * @param EntityCollection $results |
|
| 47 | + * @param string $relation |
|
| 48 | + * @return array |
|
| 49 | + */ |
|
| 50 | + public function match(array $entities, EntityCollection $results, $relation) |
|
| 51 | + { |
|
| 52 | + return $this->matchMany($entities, $results, $relation); |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -7,119 +7,119 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class MorphOneOrMany extends HasOneOrMany |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The foreign key type for the relationship. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $morphType; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The class name of the parent model. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $morphClass; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Create a new has many relationship instance. |
|
| 26 | - * |
|
| 27 | - * @param Mapper $mapper |
|
| 28 | - * @param \Analogue\ORM\Mappable $parent |
|
| 29 | - * @param string $type |
|
| 30 | - * @param string $id |
|
| 31 | - * @param string $localKey |
|
| 32 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 33 | - */ |
|
| 34 | - public function __construct(Mapper $mapper, $parent, $type, $id, $localKey) |
|
| 35 | - { |
|
| 36 | - $this->morphType = $type; |
|
| 37 | - $this->morphClass = $mapper->getManager()->getMapper($parent)->getEntityMap()->getMorphClass(); |
|
| 38 | - |
|
| 39 | - parent::__construct($mapper, $parent, $id, $localKey); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Set the base constraints on the relation query. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function addConstraints() |
|
| 48 | - { |
|
| 49 | - if (static::$constraints) { |
|
| 50 | - parent::addConstraints(); |
|
| 51 | - |
|
| 52 | - $this->query->where($this->morphType, $this->morphClass); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Get the relationship count query. |
|
| 58 | - * |
|
| 59 | - * @param Query $query |
|
| 60 | - * @param Query $parent |
|
| 61 | - * @return Query |
|
| 62 | - */ |
|
| 63 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 64 | - { |
|
| 65 | - $query = parent::getRelationCountQuery($query, $parent); |
|
| 66 | - |
|
| 67 | - return $query->where($this->morphType, $this->morphClass); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Set the constraints for an eager load of the relation. |
|
| 72 | - * |
|
| 73 | - * @param array $entities |
|
| 74 | - * @return void |
|
| 75 | - */ |
|
| 76 | - public function addEagerConstraints(array $entities) |
|
| 77 | - { |
|
| 78 | - parent::addEagerConstraints($entities); |
|
| 79 | - |
|
| 80 | - $this->query->where($this->morphType, $this->morphClass); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get the foreign key "type" name. |
|
| 85 | - * |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getMorphType() |
|
| 89 | - { |
|
| 90 | - return $this->morphType; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the plain morph type name without the table. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function getPlainMorphType() |
|
| 99 | - { |
|
| 100 | - return last(explode('.', $this->morphType)); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get the class name of the parent model. |
|
| 105 | - * |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function getMorphClass() |
|
| 109 | - { |
|
| 110 | - return $this->morphClass; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the foreign key as value pair for this relation |
|
| 115 | - * |
|
| 116 | - * @return array |
|
| 117 | - */ |
|
| 118 | - public function getForeignKeyValuePair() |
|
| 119 | - { |
|
| 120 | - return [ |
|
| 121 | - $this->getPlainForeignKey() => $this->getParentKey(), |
|
| 122 | - $this->getPlainMorphType() => $this->morphClass, |
|
| 123 | - ]; |
|
| 124 | - } |
|
| 10 | + /** |
|
| 11 | + * The foreign key type for the relationship. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $morphType; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The class name of the parent model. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $morphClass; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Create a new has many relationship instance. |
|
| 26 | + * |
|
| 27 | + * @param Mapper $mapper |
|
| 28 | + * @param \Analogue\ORM\Mappable $parent |
|
| 29 | + * @param string $type |
|
| 30 | + * @param string $id |
|
| 31 | + * @param string $localKey |
|
| 32 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 33 | + */ |
|
| 34 | + public function __construct(Mapper $mapper, $parent, $type, $id, $localKey) |
|
| 35 | + { |
|
| 36 | + $this->morphType = $type; |
|
| 37 | + $this->morphClass = $mapper->getManager()->getMapper($parent)->getEntityMap()->getMorphClass(); |
|
| 38 | + |
|
| 39 | + parent::__construct($mapper, $parent, $id, $localKey); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Set the base constraints on the relation query. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function addConstraints() |
|
| 48 | + { |
|
| 49 | + if (static::$constraints) { |
|
| 50 | + parent::addConstraints(); |
|
| 51 | + |
|
| 52 | + $this->query->where($this->morphType, $this->morphClass); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Get the relationship count query. |
|
| 58 | + * |
|
| 59 | + * @param Query $query |
|
| 60 | + * @param Query $parent |
|
| 61 | + * @return Query |
|
| 62 | + */ |
|
| 63 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 64 | + { |
|
| 65 | + $query = parent::getRelationCountQuery($query, $parent); |
|
| 66 | + |
|
| 67 | + return $query->where($this->morphType, $this->morphClass); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Set the constraints for an eager load of the relation. |
|
| 72 | + * |
|
| 73 | + * @param array $entities |
|
| 74 | + * @return void |
|
| 75 | + */ |
|
| 76 | + public function addEagerConstraints(array $entities) |
|
| 77 | + { |
|
| 78 | + parent::addEagerConstraints($entities); |
|
| 79 | + |
|
| 80 | + $this->query->where($this->morphType, $this->morphClass); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get the foreign key "type" name. |
|
| 85 | + * |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getMorphType() |
|
| 89 | + { |
|
| 90 | + return $this->morphType; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the plain morph type name without the table. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function getPlainMorphType() |
|
| 99 | + { |
|
| 100 | + return last(explode('.', $this->morphType)); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get the class name of the parent model. |
|
| 105 | + * |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function getMorphClass() |
|
| 109 | + { |
|
| 110 | + return $this->morphClass; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the foreign key as value pair for this relation |
|
| 115 | + * |
|
| 116 | + * @return array |
|
| 117 | + */ |
|
| 118 | + public function getForeignKeyValuePair() |
|
| 119 | + { |
|
| 120 | + return [ |
|
| 121 | + $this->getPlainForeignKey() => $this->getParentKey(), |
|
| 122 | + $this->getPlainMorphType() => $this->morphClass, |
|
| 123 | + ]; |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | // of the related models matching on the foreign key that's on a parent. |
| 104 | 104 | $table = $this->relatedMap->getTable(); |
| 105 | 105 | |
| 106 | - $this->query->where($table . '.' . $this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
| 106 | + $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | { |
| 119 | 119 | $query->select(new Expression('count(*)')); |
| 120 | 120 | |
| 121 | - $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey); |
|
| 121 | + $otherKey = $this->wrap($query->getTable().'.'.$this->otherKey); |
|
| 122 | 122 | |
| 123 | 123 | return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); |
| 124 | 124 | } |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | // We'll grab the primary key name of the related models since it could be set to |
| 135 | 135 | // a non-standard name and not "id". We will then construct the constraint for |
| 136 | 136 | // our eagerly loading query so it returns the proper models from execution. |
| 137 | - $key = $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 137 | + $key = $this->relatedMap->getTable().'.'.$this->otherKey; |
|
| 138 | 138 | |
| 139 | 139 | $this->query->whereIn($key, $this->getEagerModelKeys($entities)); |
| 140 | 140 | } |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | public function getQualifiedForeignKey() |
| 299 | 299 | { |
| 300 | - return $this->parentMap->getTable() . '.' . $this->foreignKey; |
|
| 300 | + return $this->parentMap->getTable().'.'.$this->foreignKey; |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
@@ -317,6 +317,6 @@ discard block |
||
| 317 | 317 | */ |
| 318 | 318 | public function getQualifiedOtherKeyName() |
| 319 | 319 | { |
| 320 | - return $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 320 | + return $this->relatedMap->getTable().'.'.$this->otherKey; |
|
| 321 | 321 | } |
| 322 | 322 | } |
@@ -10,319 +10,319 @@ |
||
| 10 | 10 | |
| 11 | 11 | class BelongsTo extends Relationship |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The foreign key of the parent model. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $foreignKey; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The associated key on the parent model. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $otherKey; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The name of the relationship. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - protected $relation; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Indicate if the parent entity hold the key for the relation. |
|
| 36 | - * |
|
| 37 | - * @var boolean |
|
| 38 | - */ |
|
| 39 | - protected static $ownForeignKey = true; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Create a new belongs to relationship instance. |
|
| 43 | - * |
|
| 44 | - * @param Mapper $mapper |
|
| 45 | - * @param Mappable $parent |
|
| 46 | - * @param string $foreignKey |
|
| 47 | - * @param string $otherKey |
|
| 48 | - * @param string $relation |
|
| 49 | - */ |
|
| 50 | - public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) |
|
| 51 | - { |
|
| 52 | - $this->otherKey = $otherKey; |
|
| 53 | - $this->relation = $relation; |
|
| 54 | - $this->foreignKey = $foreignKey; |
|
| 55 | - |
|
| 56 | - parent::__construct($mapper, $parent); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param $related |
|
| 61 | - * @return mixed |
|
| 62 | - */ |
|
| 63 | - public function attachTo($related) |
|
| 64 | - { |
|
| 65 | - $this->associate($related); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param $related |
|
| 70 | - * @return Mappable |
|
| 71 | - */ |
|
| 72 | - public function detachFrom($related) |
|
| 73 | - { |
|
| 74 | - return $this->dissociate($related); //todo |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the results of the relationship. |
|
| 79 | - * |
|
| 80 | - * @param $relation |
|
| 81 | - * |
|
| 82 | - * @return \Analogue\ORM\Entity |
|
| 83 | - */ |
|
| 84 | - public function getResults($relation) |
|
| 85 | - { |
|
| 86 | - $result = $this->query->first(); |
|
| 87 | - |
|
| 88 | - $this->cacheRelation($result, $relation); |
|
| 89 | - |
|
| 90 | - return $result; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Set the base constraints on the relation query. |
|
| 95 | - * |
|
| 96 | - * @return void |
|
| 97 | - */ |
|
| 98 | - public function addConstraints() |
|
| 99 | - { |
|
| 100 | - if (static::$constraints) { |
|
| 101 | - // For belongs to relationships, which are essentially the inverse of has one |
|
| 102 | - // or has many relationships, we need to actually query on the primary key |
|
| 103 | - // of the related models matching on the foreign key that's on a parent. |
|
| 104 | - $table = $this->relatedMap->getTable(); |
|
| 105 | - |
|
| 106 | - $this->query->where($table . '.' . $this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Add the constraints for a relationship count query. |
|
| 112 | - * |
|
| 113 | - * @param Query $query |
|
| 114 | - * @param Query $parent |
|
| 115 | - * @return Query |
|
| 116 | - */ |
|
| 117 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 118 | - { |
|
| 119 | - $query->select(new Expression('count(*)')); |
|
| 120 | - |
|
| 121 | - $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey); |
|
| 122 | - |
|
| 123 | - return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Set the constraints for an eager load of the relation. |
|
| 128 | - * |
|
| 129 | - * @param array $entities |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public function addEagerConstraints(array $entities) |
|
| 133 | - { |
|
| 134 | - // We'll grab the primary key name of the related models since it could be set to |
|
| 135 | - // a non-standard name and not "id". We will then construct the constraint for |
|
| 136 | - // our eagerly loading query so it returns the proper models from execution. |
|
| 137 | - $key = $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 138 | - |
|
| 139 | - $this->query->whereIn($key, $this->getEagerModelKeys($entities)); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Gather the keys from an array of related models. |
|
| 144 | - * |
|
| 145 | - * @param array $entities |
|
| 146 | - * @return array |
|
| 147 | - */ |
|
| 148 | - protected function getEagerModelKeys(array $entities) |
|
| 149 | - { |
|
| 150 | - $keys = []; |
|
| 151 | - |
|
| 152 | - // First we need to gather all of the keys from the parent models so we know what |
|
| 153 | - // to query for via the eager loading query. We will add them to an array then |
|
| 154 | - // execute a "where in" statement to gather up all of those related records. |
|
| 155 | - foreach ($entities as $entity) { |
|
| 156 | - $entity = $this->factory->make($entity); |
|
| 157 | - |
|
| 158 | - if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
| 159 | - $keys[] = $value; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // If there are no keys that were not null we will just return an array with 0 in |
|
| 164 | - // it so the query doesn't fail, but will not return any results, which should |
|
| 165 | - // be what this developer is expecting in a case where this happens to them. |
|
| 166 | - if (count($keys) == 0) { |
|
| 167 | - return [0]; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - return array_values(array_unique($keys)); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Initialize the relation on a set of models. |
|
| 175 | - * |
|
| 176 | - * @param array $entities |
|
| 177 | - * @param string $relation |
|
| 178 | - * @return array |
|
| 179 | - */ |
|
| 180 | - public function initRelation(array $entities, $relation) |
|
| 181 | - { |
|
| 182 | - foreach ($entities as $entity) { |
|
| 183 | - $entity = $this->factory->make($entity); |
|
| 184 | - $entity->setEntityAttribute($relation, null); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $entities; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Match the eagerly loaded results to their parents. |
|
| 192 | - * |
|
| 193 | - * @param array $entities |
|
| 194 | - * @param EntityCollection $results |
|
| 195 | - * @param string $relation |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - public function match(array $entities, EntityCollection $results, $relation) |
|
| 199 | - { |
|
| 200 | - $foreign = $this->foreignKey; |
|
| 201 | - |
|
| 202 | - $other = $this->otherKey; |
|
| 203 | - |
|
| 204 | - // First we will get to build a dictionary of the child models by their primary |
|
| 205 | - // key of the relationship, then we can easily match the children back onto |
|
| 206 | - // the parents using that dictionary and the primary key of the children. |
|
| 207 | - $dictionary = []; |
|
| 208 | - |
|
| 209 | - foreach ($results as $result) { |
|
| 210 | - $result = $this->factory->make($result); |
|
| 211 | - $dictionary[$result->getEntityAttribute($other)] = $result->getObject(); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - // Once we have the dictionary constructed, we can loop through all the parents |
|
| 215 | - // and match back onto their children using these keys of the dictionary and |
|
| 216 | - // the primary key of the children to map them onto the correct instances. |
|
| 217 | - foreach ($entities as $entity) { |
|
| 218 | - $entity = $this->factory->make($entity); |
|
| 219 | - |
|
| 220 | - if (isset($dictionary[$entity->getEntityAttribute($foreign)])) { |
|
| 221 | - $entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return $entities; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - public function sync(array $entities) |
|
| 229 | - { |
|
| 230 | - if (count($entities) > 1) { |
|
| 231 | - throw new MappingException("Single Relationship shouldn't be synced with more than one entity"); |
|
| 232 | - } |
|
| 13 | + /** |
|
| 14 | + * The foreign key of the parent model. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $foreignKey; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The associated key on the parent model. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $otherKey; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The name of the relationship. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + protected $relation; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Indicate if the parent entity hold the key for the relation. |
|
| 36 | + * |
|
| 37 | + * @var boolean |
|
| 38 | + */ |
|
| 39 | + protected static $ownForeignKey = true; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Create a new belongs to relationship instance. |
|
| 43 | + * |
|
| 44 | + * @param Mapper $mapper |
|
| 45 | + * @param Mappable $parent |
|
| 46 | + * @param string $foreignKey |
|
| 47 | + * @param string $otherKey |
|
| 48 | + * @param string $relation |
|
| 49 | + */ |
|
| 50 | + public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) |
|
| 51 | + { |
|
| 52 | + $this->otherKey = $otherKey; |
|
| 53 | + $this->relation = $relation; |
|
| 54 | + $this->foreignKey = $foreignKey; |
|
| 55 | + |
|
| 56 | + parent::__construct($mapper, $parent); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param $related |
|
| 61 | + * @return mixed |
|
| 62 | + */ |
|
| 63 | + public function attachTo($related) |
|
| 64 | + { |
|
| 65 | + $this->associate($related); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param $related |
|
| 70 | + * @return Mappable |
|
| 71 | + */ |
|
| 72 | + public function detachFrom($related) |
|
| 73 | + { |
|
| 74 | + return $this->dissociate($related); //todo |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the results of the relationship. |
|
| 79 | + * |
|
| 80 | + * @param $relation |
|
| 81 | + * |
|
| 82 | + * @return \Analogue\ORM\Entity |
|
| 83 | + */ |
|
| 84 | + public function getResults($relation) |
|
| 85 | + { |
|
| 86 | + $result = $this->query->first(); |
|
| 87 | + |
|
| 88 | + $this->cacheRelation($result, $relation); |
|
| 89 | + |
|
| 90 | + return $result; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Set the base constraints on the relation query. |
|
| 95 | + * |
|
| 96 | + * @return void |
|
| 97 | + */ |
|
| 98 | + public function addConstraints() |
|
| 99 | + { |
|
| 100 | + if (static::$constraints) { |
|
| 101 | + // For belongs to relationships, which are essentially the inverse of has one |
|
| 102 | + // or has many relationships, we need to actually query on the primary key |
|
| 103 | + // of the related models matching on the foreign key that's on a parent. |
|
| 104 | + $table = $this->relatedMap->getTable(); |
|
| 105 | + |
|
| 106 | + $this->query->where($table . '.' . $this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Add the constraints for a relationship count query. |
|
| 112 | + * |
|
| 113 | + * @param Query $query |
|
| 114 | + * @param Query $parent |
|
| 115 | + * @return Query |
|
| 116 | + */ |
|
| 117 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 118 | + { |
|
| 119 | + $query->select(new Expression('count(*)')); |
|
| 120 | + |
|
| 121 | + $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey); |
|
| 122 | + |
|
| 123 | + return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Set the constraints for an eager load of the relation. |
|
| 128 | + * |
|
| 129 | + * @param array $entities |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public function addEagerConstraints(array $entities) |
|
| 133 | + { |
|
| 134 | + // We'll grab the primary key name of the related models since it could be set to |
|
| 135 | + // a non-standard name and not "id". We will then construct the constraint for |
|
| 136 | + // our eagerly loading query so it returns the proper models from execution. |
|
| 137 | + $key = $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 138 | + |
|
| 139 | + $this->query->whereIn($key, $this->getEagerModelKeys($entities)); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Gather the keys from an array of related models. |
|
| 144 | + * |
|
| 145 | + * @param array $entities |
|
| 146 | + * @return array |
|
| 147 | + */ |
|
| 148 | + protected function getEagerModelKeys(array $entities) |
|
| 149 | + { |
|
| 150 | + $keys = []; |
|
| 151 | + |
|
| 152 | + // First we need to gather all of the keys from the parent models so we know what |
|
| 153 | + // to query for via the eager loading query. We will add them to an array then |
|
| 154 | + // execute a "where in" statement to gather up all of those related records. |
|
| 155 | + foreach ($entities as $entity) { |
|
| 156 | + $entity = $this->factory->make($entity); |
|
| 157 | + |
|
| 158 | + if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
| 159 | + $keys[] = $value; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // If there are no keys that were not null we will just return an array with 0 in |
|
| 164 | + // it so the query doesn't fail, but will not return any results, which should |
|
| 165 | + // be what this developer is expecting in a case where this happens to them. |
|
| 166 | + if (count($keys) == 0) { |
|
| 167 | + return [0]; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + return array_values(array_unique($keys)); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Initialize the relation on a set of models. |
|
| 175 | + * |
|
| 176 | + * @param array $entities |
|
| 177 | + * @param string $relation |
|
| 178 | + * @return array |
|
| 179 | + */ |
|
| 180 | + public function initRelation(array $entities, $relation) |
|
| 181 | + { |
|
| 182 | + foreach ($entities as $entity) { |
|
| 183 | + $entity = $this->factory->make($entity); |
|
| 184 | + $entity->setEntityAttribute($relation, null); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $entities; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Match the eagerly loaded results to their parents. |
|
| 192 | + * |
|
| 193 | + * @param array $entities |
|
| 194 | + * @param EntityCollection $results |
|
| 195 | + * @param string $relation |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + public function match(array $entities, EntityCollection $results, $relation) |
|
| 199 | + { |
|
| 200 | + $foreign = $this->foreignKey; |
|
| 201 | + |
|
| 202 | + $other = $this->otherKey; |
|
| 203 | + |
|
| 204 | + // First we will get to build a dictionary of the child models by their primary |
|
| 205 | + // key of the relationship, then we can easily match the children back onto |
|
| 206 | + // the parents using that dictionary and the primary key of the children. |
|
| 207 | + $dictionary = []; |
|
| 208 | + |
|
| 209 | + foreach ($results as $result) { |
|
| 210 | + $result = $this->factory->make($result); |
|
| 211 | + $dictionary[$result->getEntityAttribute($other)] = $result->getObject(); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + // Once we have the dictionary constructed, we can loop through all the parents |
|
| 215 | + // and match back onto their children using these keys of the dictionary and |
|
| 216 | + // the primary key of the children to map them onto the correct instances. |
|
| 217 | + foreach ($entities as $entity) { |
|
| 218 | + $entity = $this->factory->make($entity); |
|
| 219 | + |
|
| 220 | + if (isset($dictionary[$entity->getEntityAttribute($foreign)])) { |
|
| 221 | + $entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return $entities; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + public function sync(array $entities) |
|
| 229 | + { |
|
| 230 | + if (count($entities) > 1) { |
|
| 231 | + throw new MappingException("Single Relationship shouldn't be synced with more than one entity"); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - if (count($entities) == 1) { |
|
| 235 | - return $this->associate($entities[0]); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return false; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Associate the model instance to the given parent. |
|
| 243 | - * |
|
| 244 | - * @param mixed $entity |
|
| 245 | - * @return void |
|
| 246 | - */ |
|
| 247 | - public function associate($entity) |
|
| 248 | - { |
|
| 249 | - $this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Dissociate previously associated model from the given parent. |
|
| 254 | - * |
|
| 255 | - * @return Mappable |
|
| 256 | - */ |
|
| 257 | - public function dissociate() |
|
| 258 | - { |
|
| 259 | - // The Mapper will retrieve this association within the object model, we won't be using |
|
| 260 | - // the foreign key attribute inside the parent Entity. |
|
| 261 | - // |
|
| 262 | - //$this->parent->setEntityAttribute($this->foreignKey, null); |
|
| 263 | - |
|
| 264 | - $this->parent->setEntityAttribute($this->relation, null); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Get the foreign key of the relationship. |
|
| 269 | - * |
|
| 270 | - * @return string |
|
| 271 | - */ |
|
| 272 | - public function getForeignKey() |
|
| 273 | - { |
|
| 274 | - return $this->foreignKey; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Get the foreign key value pair for a related object |
|
| 279 | - * |
|
| 280 | - * @param mixed $related |
|
| 281 | - * |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function getForeignKeyValuePair($related) |
|
| 285 | - { |
|
| 286 | - $foreignKey = $this->getForeignKey(); |
|
| 287 | - |
|
| 288 | - if ($related) { |
|
| 289 | - $wrapper = $this->factory->make($related); |
|
| 290 | - |
|
| 291 | - $relatedKey = $this->relatedMap->getKeyName(); |
|
| 292 | - |
|
| 293 | - return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)]; |
|
| 294 | - } else { |
|
| 295 | - return [$foreignKey => null]; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Get the fully qualified foreign key of the relationship. |
|
| 301 | - * |
|
| 302 | - * @return string |
|
| 303 | - */ |
|
| 304 | - public function getQualifiedForeignKey() |
|
| 305 | - { |
|
| 306 | - return $this->parentMap->getTable() . '.' . $this->foreignKey; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get the associated key of the relationship. |
|
| 311 | - * |
|
| 312 | - * @return string |
|
| 313 | - */ |
|
| 314 | - public function getOtherKey() |
|
| 315 | - { |
|
| 316 | - return $this->otherKey; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the fully qualified associated key of the relationship. |
|
| 321 | - * |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - public function getQualifiedOtherKeyName() |
|
| 325 | - { |
|
| 326 | - return $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 327 | - } |
|
| 234 | + if (count($entities) == 1) { |
|
| 235 | + return $this->associate($entities[0]); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return false; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Associate the model instance to the given parent. |
|
| 243 | + * |
|
| 244 | + * @param mixed $entity |
|
| 245 | + * @return void |
|
| 246 | + */ |
|
| 247 | + public function associate($entity) |
|
| 248 | + { |
|
| 249 | + $this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Dissociate previously associated model from the given parent. |
|
| 254 | + * |
|
| 255 | + * @return Mappable |
|
| 256 | + */ |
|
| 257 | + public function dissociate() |
|
| 258 | + { |
|
| 259 | + // The Mapper will retrieve this association within the object model, we won't be using |
|
| 260 | + // the foreign key attribute inside the parent Entity. |
|
| 261 | + // |
|
| 262 | + //$this->parent->setEntityAttribute($this->foreignKey, null); |
|
| 263 | + |
|
| 264 | + $this->parent->setEntityAttribute($this->relation, null); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Get the foreign key of the relationship. |
|
| 269 | + * |
|
| 270 | + * @return string |
|
| 271 | + */ |
|
| 272 | + public function getForeignKey() |
|
| 273 | + { |
|
| 274 | + return $this->foreignKey; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Get the foreign key value pair for a related object |
|
| 279 | + * |
|
| 280 | + * @param mixed $related |
|
| 281 | + * |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function getForeignKeyValuePair($related) |
|
| 285 | + { |
|
| 286 | + $foreignKey = $this->getForeignKey(); |
|
| 287 | + |
|
| 288 | + if ($related) { |
|
| 289 | + $wrapper = $this->factory->make($related); |
|
| 290 | + |
|
| 291 | + $relatedKey = $this->relatedMap->getKeyName(); |
|
| 292 | + |
|
| 293 | + return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)]; |
|
| 294 | + } else { |
|
| 295 | + return [$foreignKey => null]; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Get the fully qualified foreign key of the relationship. |
|
| 301 | + * |
|
| 302 | + * @return string |
|
| 303 | + */ |
|
| 304 | + public function getQualifiedForeignKey() |
|
| 305 | + { |
|
| 306 | + return $this->parentMap->getTable() . '.' . $this->foreignKey; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get the associated key of the relationship. |
|
| 311 | + * |
|
| 312 | + * @return string |
|
| 313 | + */ |
|
| 314 | + public function getOtherKey() |
|
| 315 | + { |
|
| 316 | + return $this->otherKey; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the fully qualified associated key of the relationship. |
|
| 321 | + * |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + public function getQualifiedOtherKeyName() |
|
| 325 | + { |
|
| 326 | + return $this->relatedMap->getTable() . '.' . $this->otherKey; |
|
| 327 | + } |
|
| 328 | 328 | } |
@@ -7,167 +7,167 @@ |
||
| 7 | 7 | |
| 8 | 8 | class MorphToMany extends BelongsToMany |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The type of the polymorphic relation. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $morphType; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The class name of the morph type constraint. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $morphClass; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Indicates if we are connecting the inverse of the relation. |
|
| 26 | - * |
|
| 27 | - * This primarily affects the morphClass constraint. |
|
| 28 | - * |
|
| 29 | - * @var bool |
|
| 30 | - */ |
|
| 31 | - protected $inverse; |
|
| 32 | - |
|
| 33 | - protected static $hasPivot = true; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Create a new has many relationship instance. |
|
| 37 | - * |
|
| 38 | - * @param Mapper $mapper |
|
| 39 | - * @param \Analogue\ORM\Entity $parent |
|
| 40 | - * @param string $name |
|
| 41 | - * @param string $table |
|
| 42 | - * @param string $foreignKey |
|
| 43 | - * @param string $otherKey |
|
| 44 | - * @param string|null $relationName |
|
| 45 | - * @param bool $inverse |
|
| 46 | - */ |
|
| 47 | - public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) |
|
| 48 | - { |
|
| 49 | - $this->inverse = $inverse; |
|
| 10 | + /** |
|
| 11 | + * The type of the polymorphic relation. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $morphType; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The class name of the morph type constraint. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $morphClass; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Indicates if we are connecting the inverse of the relation. |
|
| 26 | + * |
|
| 27 | + * This primarily affects the morphClass constraint. |
|
| 28 | + * |
|
| 29 | + * @var bool |
|
| 30 | + */ |
|
| 31 | + protected $inverse; |
|
| 32 | + |
|
| 33 | + protected static $hasPivot = true; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Create a new has many relationship instance. |
|
| 37 | + * |
|
| 38 | + * @param Mapper $mapper |
|
| 39 | + * @param \Analogue\ORM\Entity $parent |
|
| 40 | + * @param string $name |
|
| 41 | + * @param string $table |
|
| 42 | + * @param string $foreignKey |
|
| 43 | + * @param string $otherKey |
|
| 44 | + * @param string|null $relationName |
|
| 45 | + * @param bool $inverse |
|
| 46 | + */ |
|
| 47 | + public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) |
|
| 48 | + { |
|
| 49 | + $this->inverse = $inverse; |
|
| 50 | 50 | |
| 51 | - $this->morphType = $name . '_type'; |
|
| 52 | - |
|
| 53 | - $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent); |
|
| 54 | - |
|
| 55 | - parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Set the where clause for the relation query. |
|
| 60 | - * |
|
| 61 | - * @return self |
|
| 62 | - */ |
|
| 63 | - protected function setWhere() |
|
| 64 | - { |
|
| 65 | - parent::setWhere(); |
|
| 66 | - |
|
| 67 | - $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 68 | - |
|
| 69 | - return $this; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Add the constraints for a relationship count query. |
|
| 74 | - * |
|
| 75 | - * @param Query $query |
|
| 76 | - * @param Query $parent |
|
| 77 | - * @return Query |
|
| 78 | - */ |
|
| 79 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 80 | - { |
|
| 81 | - $query = parent::getRelationCountQuery($query, $parent); |
|
| 82 | - |
|
| 83 | - return $query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Set the constraints for an eager load of the relation. |
|
| 88 | - * |
|
| 89 | - * @param array $entities |
|
| 90 | - * @return void |
|
| 91 | - */ |
|
| 92 | - public function addEagerConstraints(array $entities) |
|
| 93 | - { |
|
| 94 | - parent::addEagerConstraints($entities); |
|
| 95 | - |
|
| 96 | - $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Create a new pivot attachment record. |
|
| 101 | - * |
|
| 102 | - * @param int $id |
|
| 103 | - * @param bool $timed |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - protected function createAttachRecord($id, $timed) |
|
| 107 | - { |
|
| 108 | - $record = parent::createAttachRecord($id, $timed); |
|
| 109 | - |
|
| 110 | - return array_add($record, $this->morphType, $this->morphClass); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Create a new query builder for the pivot table. |
|
| 115 | - * |
|
| 116 | - * @throws \InvalidArgumentException |
|
| 117 | - * @return \Illuminate\Database\Query\Builder |
|
| 118 | - */ |
|
| 119 | - protected function newPivotQuery() |
|
| 120 | - { |
|
| 121 | - $query = parent::newPivotQuery(); |
|
| 122 | - |
|
| 123 | - return $query->where($this->morphType, $this->morphClass); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Create a new pivot model instance. |
|
| 128 | - * |
|
| 129 | - * @param array $attributes |
|
| 130 | - * @param bool $exists |
|
| 131 | - * @return Pivot |
|
| 132 | - */ |
|
| 133 | - public function newPivot(array $attributes = [], $exists = false) |
|
| 134 | - { |
|
| 135 | - $pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists); |
|
| 136 | - |
|
| 137 | - $pivot->setPivotKeys($this->foreignKey, $this->otherKey) |
|
| 138 | - ->setMorphType($this->morphType) |
|
| 139 | - ->setMorphClass($this->morphClass); |
|
| 140 | - |
|
| 141 | - return $pivot; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Return Pivot attributes when available on a relationship |
|
| 146 | - * |
|
| 147 | - * @return array |
|
| 148 | - */ |
|
| 149 | - public function getPivotAttributes() |
|
| 150 | - { |
|
| 151 | - return $this->pivotColumns; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the foreign key "type" name. |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function getMorphType() |
|
| 160 | - { |
|
| 161 | - return $this->morphType; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Get the class name of the parent model. |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function getMorphClass() |
|
| 170 | - { |
|
| 171 | - return $this->morphClass; |
|
| 172 | - } |
|
| 51 | + $this->morphType = $name . '_type'; |
|
| 52 | + |
|
| 53 | + $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent); |
|
| 54 | + |
|
| 55 | + parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Set the where clause for the relation query. |
|
| 60 | + * |
|
| 61 | + * @return self |
|
| 62 | + */ |
|
| 63 | + protected function setWhere() |
|
| 64 | + { |
|
| 65 | + parent::setWhere(); |
|
| 66 | + |
|
| 67 | + $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 68 | + |
|
| 69 | + return $this; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Add the constraints for a relationship count query. |
|
| 74 | + * |
|
| 75 | + * @param Query $query |
|
| 76 | + * @param Query $parent |
|
| 77 | + * @return Query |
|
| 78 | + */ |
|
| 79 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 80 | + { |
|
| 81 | + $query = parent::getRelationCountQuery($query, $parent); |
|
| 82 | + |
|
| 83 | + return $query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Set the constraints for an eager load of the relation. |
|
| 88 | + * |
|
| 89 | + * @param array $entities |
|
| 90 | + * @return void |
|
| 91 | + */ |
|
| 92 | + public function addEagerConstraints(array $entities) |
|
| 93 | + { |
|
| 94 | + parent::addEagerConstraints($entities); |
|
| 95 | + |
|
| 96 | + $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Create a new pivot attachment record. |
|
| 101 | + * |
|
| 102 | + * @param int $id |
|
| 103 | + * @param bool $timed |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + protected function createAttachRecord($id, $timed) |
|
| 107 | + { |
|
| 108 | + $record = parent::createAttachRecord($id, $timed); |
|
| 109 | + |
|
| 110 | + return array_add($record, $this->morphType, $this->morphClass); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Create a new query builder for the pivot table. |
|
| 115 | + * |
|
| 116 | + * @throws \InvalidArgumentException |
|
| 117 | + * @return \Illuminate\Database\Query\Builder |
|
| 118 | + */ |
|
| 119 | + protected function newPivotQuery() |
|
| 120 | + { |
|
| 121 | + $query = parent::newPivotQuery(); |
|
| 122 | + |
|
| 123 | + return $query->where($this->morphType, $this->morphClass); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Create a new pivot model instance. |
|
| 128 | + * |
|
| 129 | + * @param array $attributes |
|
| 130 | + * @param bool $exists |
|
| 131 | + * @return Pivot |
|
| 132 | + */ |
|
| 133 | + public function newPivot(array $attributes = [], $exists = false) |
|
| 134 | + { |
|
| 135 | + $pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists); |
|
| 136 | + |
|
| 137 | + $pivot->setPivotKeys($this->foreignKey, $this->otherKey) |
|
| 138 | + ->setMorphType($this->morphType) |
|
| 139 | + ->setMorphClass($this->morphClass); |
|
| 140 | + |
|
| 141 | + return $pivot; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Return Pivot attributes when available on a relationship |
|
| 146 | + * |
|
| 147 | + * @return array |
|
| 148 | + */ |
|
| 149 | + public function getPivotAttributes() |
|
| 150 | + { |
|
| 151 | + return $this->pivotColumns; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the foreign key "type" name. |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function getMorphType() |
|
| 160 | + { |
|
| 161 | + return $this->morphType; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Get the class name of the parent model. |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function getMorphClass() |
|
| 170 | + { |
|
| 171 | + return $this->morphClass; |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | $this->inverse = $inverse; |
| 50 | 50 | |
| 51 | - $this->morphType = $name . '_type'; |
|
| 51 | + $this->morphType = $name.'_type'; |
|
| 52 | 52 | |
| 53 | 53 | $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent); |
| 54 | 54 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | { |
| 65 | 65 | parent::setWhere(); |
| 66 | 66 | |
| 67 | - $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 67 | + $this->query->where($this->table.'.'.$this->morphType, $this->morphClass); |
|
| 68 | 68 | |
| 69 | 69 | return $this; |
| 70 | 70 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | { |
| 81 | 81 | $query = parent::getRelationCountQuery($query, $parent); |
| 82 | 82 | |
| 83 | - return $query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 83 | + return $query->where($this->table.'.'.$this->morphType, $this->morphClass); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | { |
| 94 | 94 | parent::addEagerConstraints($entities); |
| 95 | 95 | |
| 96 | - $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
| 96 | + $this->query->where($this->table.'.'.$this->morphType, $this->morphClass); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -7,321 +7,321 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class HasOneOrMany extends Relationship |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The foreign key of the parent model. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $foreignKey; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The local key of the parent model. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $localKey; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Create a new has many relationship instance. |
|
| 26 | - * |
|
| 27 | - * @param Mapper $mapper |
|
| 28 | - * @param \Analogue\ORM\Mappable $parentEntity |
|
| 29 | - * @param string $foreignKey |
|
| 30 | - * @param string $localKey |
|
| 31 | - */ |
|
| 32 | - public function __construct(Mapper $mapper, $parentEntity, $foreignKey, $localKey) |
|
| 33 | - { |
|
| 34 | - $this->localKey = $localKey; |
|
| 35 | - $this->foreignKey = $foreignKey; |
|
| 36 | - |
|
| 37 | - parent::__construct($mapper, $parentEntity); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param \Analogue\ORM\Entity|EntityCollection $entity |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function attachTo($entity) |
|
| 45 | - { |
|
| 46 | - if ($entity instanceof EntityCollection) { |
|
| 47 | - $this->attachMany($entity); |
|
| 48 | - } |
|
| 49 | - $this->attachOne($entity); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param $entityHash |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function detachFrom($entityHash) |
|
| 57 | - { |
|
| 58 | - if (is_array($entityHash)) { |
|
| 59 | - $this->detachMany($entityHash); |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - $this->detachMany([$entityHash]); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param \Analogue\ORM\Entity $entity |
|
| 67 | - */ |
|
| 68 | - public function attachOne($entity) |
|
| 69 | - { |
|
| 70 | - $wrapper = $this->factory->make($entity); |
|
| 71 | - |
|
| 72 | - // Ok, we need to guess the inverse of the relation from there. |
|
| 73 | - // Let's assume the inverse of the relation method is the name of |
|
| 74 | - // the entity. |
|
| 75 | - |
|
| 76 | - $wrapper->setEntityAttribute($this->getPlainForeignKey(), $this->getParentKey()); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param EntityCollection $entities |
|
| 81 | - */ |
|
| 82 | - public function attachMany(EntityCollection $entities) |
|
| 83 | - { |
|
| 84 | - foreach ($entities as $entity) { |
|
| 85 | - $this->attachOne($entity); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param $entityHash |
|
| 91 | - */ |
|
| 92 | - protected function detachOne($entityHash) |
|
| 93 | - { |
|
| 94 | - $this->detachMany([$entityHash]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Attach ids that are passed as arguments, and detach any other |
|
| 99 | - * @param mixed $entities |
|
| 100 | - * @throws \InvalidArgumentException |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function sync(array $entities) |
|
| 104 | - { |
|
| 105 | - $this->detachExcept($entities); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param $entities |
|
| 110 | - * @throws \InvalidArgumentException |
|
| 111 | - */ |
|
| 112 | - protected function detachExcept($entities) |
|
| 113 | - { |
|
| 114 | - $query = $this->query->getQuery()->from($this->relatedMap->getTable()); |
|
| 115 | - |
|
| 116 | - if (count($entities) > 0) { |
|
| 117 | - $keys = $this->getKeys($entities); |
|
| 118 | - $query->whereNotIn($this->relatedMap->getKeyName(), $keys); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $parentKey = $this->parentMap->getKeyName(); |
|
| 122 | - |
|
| 123 | - $query->where($this->getPlainForeignKey(), '=', $this->parent->getEntityAttribute($parentKey)) |
|
| 124 | - ->update([$this->getPlainForeignKey() => null]); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param array $entityHashes |
|
| 129 | - */ |
|
| 130 | - public function detachMany(array $entityHashes) |
|
| 131 | - { |
|
| 132 | - $keys = []; |
|
| 133 | - |
|
| 134 | - foreach ($entityHashes as $hash) { |
|
| 135 | - $split = explode('.', $hash); |
|
| 136 | - $keys[] = $split[1]; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $query = $this->query->getQuery()->from($this->relatedMap->getTable()); |
|
| 140 | - |
|
| 141 | - $query->whereIn($this->relatedMap->getKeyName(), $keys) |
|
| 142 | - ->update([$this->getPlainForeignKey() => null]); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Set the base constraints on the relation query. |
|
| 147 | - * |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - public function addConstraints() |
|
| 151 | - { |
|
| 152 | - if (static::$constraints) { |
|
| 153 | - $this->query->where($this->foreignKey, '=', $this->getParentKey()); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Set the constraints for an eager load of the relation. |
|
| 159 | - * |
|
| 160 | - * @param array $entities |
|
| 161 | - * @return void |
|
| 162 | - */ |
|
| 163 | - public function addEagerConstraints(array $entities) |
|
| 164 | - { |
|
| 165 | - $this->query->whereIn($this->foreignKey, $this->getKeys($entities, $this->localKey)); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Match the eagerly loaded results to their single parents. |
|
| 170 | - * |
|
| 171 | - * @param array $entities |
|
| 172 | - * @param EntityCollection $results |
|
| 173 | - * @param string $relation |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function matchOne(array $entities, EntityCollection $results, $relation) |
|
| 177 | - { |
|
| 178 | - return $this->matchOneOrMany($entities, $results, $relation, 'one'); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Match the eagerly loaded results to their many parents. |
|
| 183 | - * |
|
| 184 | - * @param array $entities |
|
| 185 | - * @param EntityCollection $results |
|
| 186 | - * @param string $relation |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - public function matchMany(array $entities, EntityCollection $results, $relation) |
|
| 190 | - { |
|
| 191 | - return $this->matchOneOrMany($entities, $results, $relation, 'many'); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Match the eagerly loaded results to their many parents. |
|
| 196 | - * |
|
| 197 | - * @param array $entities |
|
| 198 | - * @param EntityCollection $results |
|
| 199 | - * @param string $relation |
|
| 200 | - * @param string $type |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 203 | - protected function matchOneOrMany(array $entities, EntityCollection $results, $relation, $type) |
|
| 204 | - { |
|
| 205 | - $dictionary = $this->buildDictionary($results); |
|
| 206 | - |
|
| 207 | - $cache = $this->parentMapper->getEntityCache(); |
|
| 208 | - |
|
| 209 | - // Once we have the dictionary we can simply spin through the parent models to |
|
| 210 | - // link them up with their children using the keyed dictionary to make the |
|
| 211 | - // matching very convenient and easy work. Then we'll just return them. |
|
| 212 | - foreach ($entities as $entity) { |
|
| 213 | - $entity = $this->factory->make($entity); |
|
| 214 | - |
|
| 215 | - $key = $entity->getEntityAttribute($this->localKey); |
|
| 216 | - |
|
| 217 | - if (isset($dictionary[$key])) { |
|
| 218 | - $value = $this->getRelationValue($dictionary, $key, $type); |
|
| 219 | - |
|
| 220 | - $entity->setEntityAttribute($relation, $value); |
|
| 221 | - |
|
| 222 | - $cache->cacheLoadedRelationResult($entity, $relation, $value, $this); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return $entities; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get the value of a relationship by one or many type. |
|
| 231 | - * |
|
| 232 | - * @param array $dictionary |
|
| 233 | - * @param string $key |
|
| 234 | - * @param string $type |
|
| 235 | - * @return mixed |
|
| 236 | - */ |
|
| 237 | - protected function getRelationValue(array $dictionary, $key, $type) |
|
| 238 | - { |
|
| 239 | - $value = $dictionary[$key]; |
|
| 240 | - |
|
| 241 | - return $type == 'one' ? reset($value) : $this->relatedMap->newCollection($value); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Build model dictionary keyed by the relation's foreign key. |
|
| 246 | - * |
|
| 247 | - * @param EntityCollection $results |
|
| 248 | - * @return array |
|
| 249 | - */ |
|
| 250 | - protected function buildDictionary(EntityCollection $results) |
|
| 251 | - { |
|
| 252 | - $dictionary = []; |
|
| 253 | - |
|
| 254 | - $foreign = $this->getPlainForeignKey(); |
|
| 255 | - |
|
| 256 | - // First we will create a dictionary of models keyed by the foreign key of the |
|
| 257 | - // relationship as this will allow us to quickly access all of the related |
|
| 258 | - // models without having to do nested looping which will be quite slow. |
|
| 259 | - foreach ($results as $result) { |
|
| 260 | - $dictionary[$result->{$foreign}][] = $result; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - return $dictionary; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Get the key for comparing against the parent key in "has" query. |
|
| 268 | - * |
|
| 269 | - * @return string |
|
| 270 | - */ |
|
| 271 | - public function getHasCompareKey() |
|
| 272 | - { |
|
| 273 | - return $this->getForeignKey(); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Get the foreign key for the relationship. |
|
| 278 | - * |
|
| 279 | - * @return string |
|
| 280 | - */ |
|
| 281 | - public function getForeignKey() |
|
| 282 | - { |
|
| 283 | - return $this->foreignKey; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Get the plain foreign key. |
|
| 288 | - * |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - public function getPlainForeignKey() |
|
| 292 | - { |
|
| 293 | - $segments = explode('.', $this->getForeignKey()); |
|
| 294 | - |
|
| 295 | - return $segments[count($segments) - 1]; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Get the key value of the parent's local key. |
|
| 300 | - * |
|
| 301 | - * @return mixed |
|
| 302 | - */ |
|
| 303 | - public function getParentKey() |
|
| 304 | - { |
|
| 305 | - return $this->parent->getEntityAttribute($this->localKey); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Get the fully qualified parent key name. |
|
| 310 | - * |
|
| 311 | - * @return string |
|
| 312 | - */ |
|
| 313 | - public function getQualifiedParentKeyName() |
|
| 314 | - { |
|
| 315 | - return $this->parentMap->getTable() . '.' . $this->localKey; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Get the foreign key as value pair for this relation |
|
| 320 | - * |
|
| 321 | - * @return array |
|
| 322 | - */ |
|
| 323 | - public function getForeignKeyValuePair() |
|
| 324 | - { |
|
| 325 | - return [$this->getPlainForeignKey() => $this->getParentKey()]; |
|
| 326 | - } |
|
| 10 | + /** |
|
| 11 | + * The foreign key of the parent model. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $foreignKey; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The local key of the parent model. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $localKey; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Create a new has many relationship instance. |
|
| 26 | + * |
|
| 27 | + * @param Mapper $mapper |
|
| 28 | + * @param \Analogue\ORM\Mappable $parentEntity |
|
| 29 | + * @param string $foreignKey |
|
| 30 | + * @param string $localKey |
|
| 31 | + */ |
|
| 32 | + public function __construct(Mapper $mapper, $parentEntity, $foreignKey, $localKey) |
|
| 33 | + { |
|
| 34 | + $this->localKey = $localKey; |
|
| 35 | + $this->foreignKey = $foreignKey; |
|
| 36 | + |
|
| 37 | + parent::__construct($mapper, $parentEntity); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param \Analogue\ORM\Entity|EntityCollection $entity |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function attachTo($entity) |
|
| 45 | + { |
|
| 46 | + if ($entity instanceof EntityCollection) { |
|
| 47 | + $this->attachMany($entity); |
|
| 48 | + } |
|
| 49 | + $this->attachOne($entity); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param $entityHash |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function detachFrom($entityHash) |
|
| 57 | + { |
|
| 58 | + if (is_array($entityHash)) { |
|
| 59 | + $this->detachMany($entityHash); |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + $this->detachMany([$entityHash]); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param \Analogue\ORM\Entity $entity |
|
| 67 | + */ |
|
| 68 | + public function attachOne($entity) |
|
| 69 | + { |
|
| 70 | + $wrapper = $this->factory->make($entity); |
|
| 71 | + |
|
| 72 | + // Ok, we need to guess the inverse of the relation from there. |
|
| 73 | + // Let's assume the inverse of the relation method is the name of |
|
| 74 | + // the entity. |
|
| 75 | + |
|
| 76 | + $wrapper->setEntityAttribute($this->getPlainForeignKey(), $this->getParentKey()); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param EntityCollection $entities |
|
| 81 | + */ |
|
| 82 | + public function attachMany(EntityCollection $entities) |
|
| 83 | + { |
|
| 84 | + foreach ($entities as $entity) { |
|
| 85 | + $this->attachOne($entity); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param $entityHash |
|
| 91 | + */ |
|
| 92 | + protected function detachOne($entityHash) |
|
| 93 | + { |
|
| 94 | + $this->detachMany([$entityHash]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Attach ids that are passed as arguments, and detach any other |
|
| 99 | + * @param mixed $entities |
|
| 100 | + * @throws \InvalidArgumentException |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function sync(array $entities) |
|
| 104 | + { |
|
| 105 | + $this->detachExcept($entities); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param $entities |
|
| 110 | + * @throws \InvalidArgumentException |
|
| 111 | + */ |
|
| 112 | + protected function detachExcept($entities) |
|
| 113 | + { |
|
| 114 | + $query = $this->query->getQuery()->from($this->relatedMap->getTable()); |
|
| 115 | + |
|
| 116 | + if (count($entities) > 0) { |
|
| 117 | + $keys = $this->getKeys($entities); |
|
| 118 | + $query->whereNotIn($this->relatedMap->getKeyName(), $keys); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $parentKey = $this->parentMap->getKeyName(); |
|
| 122 | + |
|
| 123 | + $query->where($this->getPlainForeignKey(), '=', $this->parent->getEntityAttribute($parentKey)) |
|
| 124 | + ->update([$this->getPlainForeignKey() => null]); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param array $entityHashes |
|
| 129 | + */ |
|
| 130 | + public function detachMany(array $entityHashes) |
|
| 131 | + { |
|
| 132 | + $keys = []; |
|
| 133 | + |
|
| 134 | + foreach ($entityHashes as $hash) { |
|
| 135 | + $split = explode('.', $hash); |
|
| 136 | + $keys[] = $split[1]; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $query = $this->query->getQuery()->from($this->relatedMap->getTable()); |
|
| 140 | + |
|
| 141 | + $query->whereIn($this->relatedMap->getKeyName(), $keys) |
|
| 142 | + ->update([$this->getPlainForeignKey() => null]); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Set the base constraints on the relation query. |
|
| 147 | + * |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + public function addConstraints() |
|
| 151 | + { |
|
| 152 | + if (static::$constraints) { |
|
| 153 | + $this->query->where($this->foreignKey, '=', $this->getParentKey()); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Set the constraints for an eager load of the relation. |
|
| 159 | + * |
|
| 160 | + * @param array $entities |
|
| 161 | + * @return void |
|
| 162 | + */ |
|
| 163 | + public function addEagerConstraints(array $entities) |
|
| 164 | + { |
|
| 165 | + $this->query->whereIn($this->foreignKey, $this->getKeys($entities, $this->localKey)); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Match the eagerly loaded results to their single parents. |
|
| 170 | + * |
|
| 171 | + * @param array $entities |
|
| 172 | + * @param EntityCollection $results |
|
| 173 | + * @param string $relation |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function matchOne(array $entities, EntityCollection $results, $relation) |
|
| 177 | + { |
|
| 178 | + return $this->matchOneOrMany($entities, $results, $relation, 'one'); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Match the eagerly loaded results to their many parents. |
|
| 183 | + * |
|
| 184 | + * @param array $entities |
|
| 185 | + * @param EntityCollection $results |
|
| 186 | + * @param string $relation |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + public function matchMany(array $entities, EntityCollection $results, $relation) |
|
| 190 | + { |
|
| 191 | + return $this->matchOneOrMany($entities, $results, $relation, 'many'); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Match the eagerly loaded results to their many parents. |
|
| 196 | + * |
|
| 197 | + * @param array $entities |
|
| 198 | + * @param EntityCollection $results |
|
| 199 | + * @param string $relation |
|
| 200 | + * @param string $type |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | + protected function matchOneOrMany(array $entities, EntityCollection $results, $relation, $type) |
|
| 204 | + { |
|
| 205 | + $dictionary = $this->buildDictionary($results); |
|
| 206 | + |
|
| 207 | + $cache = $this->parentMapper->getEntityCache(); |
|
| 208 | + |
|
| 209 | + // Once we have the dictionary we can simply spin through the parent models to |
|
| 210 | + // link them up with their children using the keyed dictionary to make the |
|
| 211 | + // matching very convenient and easy work. Then we'll just return them. |
|
| 212 | + foreach ($entities as $entity) { |
|
| 213 | + $entity = $this->factory->make($entity); |
|
| 214 | + |
|
| 215 | + $key = $entity->getEntityAttribute($this->localKey); |
|
| 216 | + |
|
| 217 | + if (isset($dictionary[$key])) { |
|
| 218 | + $value = $this->getRelationValue($dictionary, $key, $type); |
|
| 219 | + |
|
| 220 | + $entity->setEntityAttribute($relation, $value); |
|
| 221 | + |
|
| 222 | + $cache->cacheLoadedRelationResult($entity, $relation, $value, $this); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return $entities; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get the value of a relationship by one or many type. |
|
| 231 | + * |
|
| 232 | + * @param array $dictionary |
|
| 233 | + * @param string $key |
|
| 234 | + * @param string $type |
|
| 235 | + * @return mixed |
|
| 236 | + */ |
|
| 237 | + protected function getRelationValue(array $dictionary, $key, $type) |
|
| 238 | + { |
|
| 239 | + $value = $dictionary[$key]; |
|
| 240 | + |
|
| 241 | + return $type == 'one' ? reset($value) : $this->relatedMap->newCollection($value); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Build model dictionary keyed by the relation's foreign key. |
|
| 246 | + * |
|
| 247 | + * @param EntityCollection $results |
|
| 248 | + * @return array |
|
| 249 | + */ |
|
| 250 | + protected function buildDictionary(EntityCollection $results) |
|
| 251 | + { |
|
| 252 | + $dictionary = []; |
|
| 253 | + |
|
| 254 | + $foreign = $this->getPlainForeignKey(); |
|
| 255 | + |
|
| 256 | + // First we will create a dictionary of models keyed by the foreign key of the |
|
| 257 | + // relationship as this will allow us to quickly access all of the related |
|
| 258 | + // models without having to do nested looping which will be quite slow. |
|
| 259 | + foreach ($results as $result) { |
|
| 260 | + $dictionary[$result->{$foreign}][] = $result; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + return $dictionary; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Get the key for comparing against the parent key in "has" query. |
|
| 268 | + * |
|
| 269 | + * @return string |
|
| 270 | + */ |
|
| 271 | + public function getHasCompareKey() |
|
| 272 | + { |
|
| 273 | + return $this->getForeignKey(); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Get the foreign key for the relationship. |
|
| 278 | + * |
|
| 279 | + * @return string |
|
| 280 | + */ |
|
| 281 | + public function getForeignKey() |
|
| 282 | + { |
|
| 283 | + return $this->foreignKey; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Get the plain foreign key. |
|
| 288 | + * |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + public function getPlainForeignKey() |
|
| 292 | + { |
|
| 293 | + $segments = explode('.', $this->getForeignKey()); |
|
| 294 | + |
|
| 295 | + return $segments[count($segments) - 1]; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Get the key value of the parent's local key. |
|
| 300 | + * |
|
| 301 | + * @return mixed |
|
| 302 | + */ |
|
| 303 | + public function getParentKey() |
|
| 304 | + { |
|
| 305 | + return $this->parent->getEntityAttribute($this->localKey); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Get the fully qualified parent key name. |
|
| 310 | + * |
|
| 311 | + * @return string |
|
| 312 | + */ |
|
| 313 | + public function getQualifiedParentKeyName() |
|
| 314 | + { |
|
| 315 | + return $this->parentMap->getTable() . '.' . $this->localKey; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Get the foreign key as value pair for this relation |
|
| 320 | + * |
|
| 321 | + * @return array |
|
| 322 | + */ |
|
| 323 | + public function getForeignKeyValuePair() |
|
| 324 | + { |
|
| 325 | + return [$this->getPlainForeignKey() => $this->getParentKey()]; |
|
| 326 | + } |
|
| 327 | 327 | } |
@@ -312,7 +312,7 @@ |
||
| 312 | 312 | */ |
| 313 | 313 | public function getQualifiedParentKeyName() |
| 314 | 314 | { |
| 315 | - return $this->parentMap->getTable() . '.' . $this->localKey; |
|
| 315 | + return $this->parentMap->getTable().'.'.$this->localKey; |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | /** |
@@ -6,161 +6,161 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Pivot extends Entity |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var bool |
|
| 11 | - */ |
|
| 12 | - protected $exists; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Pivot's table |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - protected $table; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The parent entity of the relationship. |
|
| 23 | - * |
|
| 24 | - * @var object |
|
| 25 | - */ |
|
| 26 | - protected $parent; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The parent entity of the relationship. |
|
| 30 | - * |
|
| 31 | - * @var \Analogue\ORM\EntityMap |
|
| 32 | - */ |
|
| 33 | - protected $parentMap; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * We may define a pivot mapping to deal with |
|
| 37 | - * soft deletes, timestamps, etc. |
|
| 38 | - * |
|
| 39 | - * @var \Analogue\ORM\EntityMap |
|
| 40 | - */ |
|
| 41 | - protected $pivotMap; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The name of the foreign key column. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $foreignKey; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * The name of the "other key" column. |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $otherKey; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The attributes that aren't mass assignable. |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $guarded = []; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Pivot uses timestamps ? |
|
| 66 | - * |
|
| 67 | - * @var boolean |
|
| 68 | - */ |
|
| 69 | - protected $timestamps; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Create a new pivot model instance. |
|
| 73 | - * |
|
| 74 | - * @param \Analogue\ORM\System\InternallyMappable $parent |
|
| 75 | - * @param \Analogue\ORM\EntityMap $parentMap |
|
| 76 | - * @param array $attributes |
|
| 77 | - * @param string $table |
|
| 78 | - * @param bool $exists |
|
| 79 | - */ |
|
| 80 | - public function __construct($parent, $parentMap, $attributes, $table, $exists = false) |
|
| 81 | - { |
|
| 82 | - // The pivot model is a "dynamic" model since we will set the tables dynamically |
|
| 83 | - // for the instance. This allows it work for any intermediate tables for the |
|
| 84 | - // many to many relationship that are defined by this developer's classes. |
|
| 85 | - $this->setEntityAttributes($attributes, true); |
|
| 86 | - |
|
| 87 | - $this->table = $table; |
|
| 88 | - |
|
| 89 | - // We store off the parent instance so we will access the timestamp column names |
|
| 90 | - // for the model, since the pivot model timestamps aren't easily configurable |
|
| 91 | - // from the developer's point of view. We can use the parents to get these. |
|
| 92 | - $this->parent = $parent; |
|
| 93 | - |
|
| 94 | - $this->parentMap = $parentMap; |
|
| 95 | - |
|
| 96 | - $this->exists = $exists; |
|
| 97 | - |
|
| 98 | - $this->timestamps = $this->hasTimestampAttributes(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the foreign key column name. |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function getForeignKey() |
|
| 107 | - { |
|
| 108 | - return $this->foreignKey; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get the "other key" column name. |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function getOtherKey() |
|
| 117 | - { |
|
| 118 | - return $this->otherKey; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Set the key names for the pivot model instance. |
|
| 123 | - * |
|
| 124 | - * @param string $foreignKey |
|
| 125 | - * @param string $otherKey |
|
| 126 | - * @return $this |
|
| 127 | - */ |
|
| 128 | - public function setPivotKeys($foreignKey, $otherKey) |
|
| 129 | - { |
|
| 130 | - $this->foreignKey = $foreignKey; |
|
| 131 | - |
|
| 132 | - $this->otherKey = $otherKey; |
|
| 133 | - |
|
| 134 | - return $this; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Determine if the pivot model has timestamp attributes. |
|
| 139 | - * |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function hasTimestampAttributes() |
|
| 143 | - { |
|
| 144 | - return array_key_exists($this->getCreatedAtColumn(), $this->attributes); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get the name of the "created at" column. |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function getCreatedAtColumn() |
|
| 153 | - { |
|
| 154 | - return $this->parentMap->getCreatedAtColumn(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Get the name of the "updated at" column. |
|
| 159 | - * |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public function getUpdatedAtColumn() |
|
| 163 | - { |
|
| 164 | - return $this->parentMap->getUpdatedAtColumn(); |
|
| 165 | - } |
|
| 9 | + /** |
|
| 10 | + * @var bool |
|
| 11 | + */ |
|
| 12 | + protected $exists; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Pivot's table |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + protected $table; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The parent entity of the relationship. |
|
| 23 | + * |
|
| 24 | + * @var object |
|
| 25 | + */ |
|
| 26 | + protected $parent; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The parent entity of the relationship. |
|
| 30 | + * |
|
| 31 | + * @var \Analogue\ORM\EntityMap |
|
| 32 | + */ |
|
| 33 | + protected $parentMap; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * We may define a pivot mapping to deal with |
|
| 37 | + * soft deletes, timestamps, etc. |
|
| 38 | + * |
|
| 39 | + * @var \Analogue\ORM\EntityMap |
|
| 40 | + */ |
|
| 41 | + protected $pivotMap; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The name of the foreign key column. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $foreignKey; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * The name of the "other key" column. |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $otherKey; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The attributes that aren't mass assignable. |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $guarded = []; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Pivot uses timestamps ? |
|
| 66 | + * |
|
| 67 | + * @var boolean |
|
| 68 | + */ |
|
| 69 | + protected $timestamps; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Create a new pivot model instance. |
|
| 73 | + * |
|
| 74 | + * @param \Analogue\ORM\System\InternallyMappable $parent |
|
| 75 | + * @param \Analogue\ORM\EntityMap $parentMap |
|
| 76 | + * @param array $attributes |
|
| 77 | + * @param string $table |
|
| 78 | + * @param bool $exists |
|
| 79 | + */ |
|
| 80 | + public function __construct($parent, $parentMap, $attributes, $table, $exists = false) |
|
| 81 | + { |
|
| 82 | + // The pivot model is a "dynamic" model since we will set the tables dynamically |
|
| 83 | + // for the instance. This allows it work for any intermediate tables for the |
|
| 84 | + // many to many relationship that are defined by this developer's classes. |
|
| 85 | + $this->setEntityAttributes($attributes, true); |
|
| 86 | + |
|
| 87 | + $this->table = $table; |
|
| 88 | + |
|
| 89 | + // We store off the parent instance so we will access the timestamp column names |
|
| 90 | + // for the model, since the pivot model timestamps aren't easily configurable |
|
| 91 | + // from the developer's point of view. We can use the parents to get these. |
|
| 92 | + $this->parent = $parent; |
|
| 93 | + |
|
| 94 | + $this->parentMap = $parentMap; |
|
| 95 | + |
|
| 96 | + $this->exists = $exists; |
|
| 97 | + |
|
| 98 | + $this->timestamps = $this->hasTimestampAttributes(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the foreign key column name. |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function getForeignKey() |
|
| 107 | + { |
|
| 108 | + return $this->foreignKey; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get the "other key" column name. |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function getOtherKey() |
|
| 117 | + { |
|
| 118 | + return $this->otherKey; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Set the key names for the pivot model instance. |
|
| 123 | + * |
|
| 124 | + * @param string $foreignKey |
|
| 125 | + * @param string $otherKey |
|
| 126 | + * @return $this |
|
| 127 | + */ |
|
| 128 | + public function setPivotKeys($foreignKey, $otherKey) |
|
| 129 | + { |
|
| 130 | + $this->foreignKey = $foreignKey; |
|
| 131 | + |
|
| 132 | + $this->otherKey = $otherKey; |
|
| 133 | + |
|
| 134 | + return $this; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Determine if the pivot model has timestamp attributes. |
|
| 139 | + * |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function hasTimestampAttributes() |
|
| 143 | + { |
|
| 144 | + return array_key_exists($this->getCreatedAtColumn(), $this->attributes); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get the name of the "created at" column. |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function getCreatedAtColumn() |
|
| 153 | + { |
|
| 154 | + return $this->parentMap->getCreatedAtColumn(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Get the name of the "updated at" column. |
|
| 159 | + * |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public function getUpdatedAtColumn() |
|
| 163 | + { |
|
| 164 | + return $this->parentMap->getUpdatedAtColumn(); |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $farParentKeyName = $this->farParentMap->getKeyName(); |
| 94 | 94 | |
| 95 | 95 | $this->query->where( |
| 96 | - $parentTable . '.' . $this->firstKey, |
|
| 96 | + $parentTable.'.'.$this->firstKey, |
|
| 97 | 97 | '=', |
| 98 | 98 | $this->farParent->getEntityAttribute($farParentKeyName) |
| 99 | 99 | ); |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | $query->select(new Expression('count(*)')); |
| 117 | 117 | |
| 118 | - $key = $this->wrap($parentTable . '.' . $this->firstKey); |
|
| 118 | + $key = $this->wrap($parentTable.'.'.$this->firstKey); |
|
| 119 | 119 | |
| 120 | 120 | return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
| 121 | 121 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | { |
| 131 | 131 | $query = $query ?: $this->query; |
| 132 | 132 | |
| 133 | - $foreignKey = $this->relatedMap->getTable() . '.' . $this->secondKey; |
|
| 133 | + $foreignKey = $this->relatedMap->getTable().'.'.$this->secondKey; |
|
| 134 | 134 | |
| 135 | 135 | $query->join($this->parentMap->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey); |
| 136 | 136 | } |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | { |
| 146 | 146 | $table = $this->parentMap->getTable(); |
| 147 | 147 | |
| 148 | - $this->query->whereIn($table . '.' . $this->firstKey, $this->getKeys($entities)); |
|
| 148 | + $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($entities)); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /** |
@@ -269,10 +269,10 @@ discard block |
||
| 269 | 269 | protected function getSelectColumns(array $columns = ['*']) |
| 270 | 270 | { |
| 271 | 271 | if ($columns == ['*']) { |
| 272 | - $columns = [$this->relatedMap->getTable() . '.*']; |
|
| 272 | + $columns = [$this->relatedMap->getTable().'.*']; |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - return array_merge($columns, [$this->parentMap->getTable() . '.' . $this->firstKey]); |
|
| 275 | + return array_merge($columns, [$this->parentMap->getTable().'.'.$this->firstKey]); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /** |
@@ -313,7 +313,6 @@ |
||
| 313 | 313 | * Run synchronization content if needed by the |
| 314 | 314 | * relation type. |
| 315 | 315 | * |
| 316 | - * @param array $actualContent |
|
| 317 | 316 | * @return void |
| 318 | 317 | */ |
| 319 | 318 | public function sync(array $entities) |
@@ -9,315 +9,315 @@ |
||
| 9 | 9 | |
| 10 | 10 | class HasManyThrough extends Relationship |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The distance parent Entity instance. |
|
| 14 | - * |
|
| 15 | - * @var \Analogue\ORM\Entity |
|
| 16 | - */ |
|
| 17 | - protected $farParent; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The far parent map instance |
|
| 21 | - * |
|
| 22 | - * @var \Analogue\ORM\EntityMap |
|
| 23 | - */ |
|
| 24 | - protected $farParentMap; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The near key on the relationship. |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - protected $firstKey; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The far key on the relationship. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $secondKey; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Create a new has many relationship instance. |
|
| 42 | - * |
|
| 43 | - * @param Mapper $mapper |
|
| 44 | - * @param \Analogue\ORM\Mappable $farParent |
|
| 45 | - * @param \Analogue\ORM\EntityMap $parentMap |
|
| 46 | - * @param string $firstKey |
|
| 47 | - * @param string $secondKey |
|
| 48 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 49 | - */ |
|
| 50 | - public function __construct(Mapper $mapper, $farParent, $parentMap, $firstKey, $secondKey) |
|
| 51 | - { |
|
| 52 | - $this->firstKey = $firstKey; |
|
| 53 | - $this->secondKey = $secondKey; |
|
| 54 | - $this->farParent = $farParent; |
|
| 55 | - |
|
| 56 | - $this->farParentMap = $this->relatedMapper->getManager()->mapper($farParent)->getEntityMap(); |
|
| 57 | - |
|
| 58 | - $parentInstance = $this->relatedMapper->getManager()->mapper($parentMap->getClass())->newInstance(); |
|
| 59 | - |
|
| 60 | - parent::__construct($mapper, $parentInstance); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param $related |
|
| 65 | - * @return mixed |
|
| 66 | - */ |
|
| 67 | - public function attachTo($related) |
|
| 68 | - { |
|
| 69 | - // N/A |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param $related |
|
| 74 | - * @return mixed |
|
| 75 | - */ |
|
| 76 | - public function detachFrom($related) |
|
| 77 | - { |
|
| 78 | - // N/A |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Set the base constraints on the relation query. |
|
| 83 | - * |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - public function addConstraints() |
|
| 87 | - { |
|
| 88 | - $parentTable = $this->parentMap->getTable(); |
|
| 89 | - |
|
| 90 | - $this->setJoin(); |
|
| 91 | - |
|
| 92 | - if (static::$constraints) { |
|
| 93 | - $farParentKeyName = $this->farParentMap->getKeyName(); |
|
| 94 | - |
|
| 95 | - $this->query->where( |
|
| 96 | - $parentTable . '.' . $this->firstKey, |
|
| 97 | - '=', |
|
| 98 | - $this->farParent->getEntityAttribute($farParentKeyName) |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Add the constraints for a relationship count query. |
|
| 105 | - * |
|
| 106 | - * @param Query $query |
|
| 107 | - * @param Query $parent |
|
| 108 | - * @return Query |
|
| 109 | - */ |
|
| 110 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 111 | - { |
|
| 112 | - $parentTable = $this->parentMap->getTable(); |
|
| 113 | - |
|
| 114 | - $this->setJoin($query); |
|
| 115 | - |
|
| 116 | - $query->select(new Expression('count(*)')); |
|
| 117 | - |
|
| 118 | - $key = $this->wrap($parentTable . '.' . $this->firstKey); |
|
| 119 | - |
|
| 120 | - return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Set the join clause on the query. |
|
| 125 | - * |
|
| 126 | - * @param null|Query $query |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - protected function setJoin(Query $query = null) |
|
| 130 | - { |
|
| 131 | - $query = $query ?: $this->query; |
|
| 132 | - |
|
| 133 | - $foreignKey = $this->relatedMap->getTable() . '.' . $this->secondKey; |
|
| 134 | - |
|
| 135 | - $query->join($this->parentMap->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Set the constraints for an eager load of the relation. |
|
| 140 | - * |
|
| 141 | - * @param array $entities |
|
| 142 | - * @return void |
|
| 143 | - */ |
|
| 144 | - public function addEagerConstraints(array $entities) |
|
| 145 | - { |
|
| 146 | - $table = $this->parentMap->getTable(); |
|
| 147 | - |
|
| 148 | - $this->query->whereIn($table . '.' . $this->firstKey, $this->getKeys($entities)); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Initialize the relation on a set of entities. |
|
| 153 | - * |
|
| 154 | - * @param \Analogue\ORM\Entity[] $entities |
|
| 155 | - * @param string $relation |
|
| 156 | - * @return \Analogue\ORM\Entity[] |
|
| 157 | - */ |
|
| 158 | - public function initRelation(array $entities, $relation) |
|
| 159 | - { |
|
| 160 | - foreach ($entities as $entity) { |
|
| 161 | - $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return $entities; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Match the eagerly loaded results to their parents. |
|
| 169 | - * |
|
| 170 | - * @param \Analogue\ORM\Entity[] $entities |
|
| 171 | - * @param EntityCollection $results |
|
| 172 | - * @param string $relation |
|
| 173 | - * @return \Analogue\ORM\Entity[] |
|
| 174 | - */ |
|
| 175 | - public function match(array $entities, EntityCollection $results, $relation) |
|
| 176 | - { |
|
| 177 | - $dictionary = $this->buildDictionary($results); |
|
| 178 | - |
|
| 179 | - $relatedKey = $this->relatedMap->getKeyName(); |
|
| 180 | - |
|
| 181 | - $cache = $this->parentMapper->getEntityCache(); |
|
| 182 | - |
|
| 183 | - // Once we have the dictionary we can simply spin through the parent entities to |
|
| 184 | - // link them up with their children using the keyed dictionary to make the |
|
| 185 | - // matching very convenient and easy work. Then we'll just return them. |
|
| 186 | - foreach ($entities as $entity) { |
|
| 187 | - $key = $entity->getEntityAttribute($relatedKey); |
|
| 188 | - |
|
| 189 | - if (isset($dictionary[$key])) { |
|
| 190 | - $value = $this->relatedMap->newCollection($dictionary[$key]); |
|
| 191 | - |
|
| 192 | - $entity->setEntityAttribute($relation, $value); |
|
| 193 | - |
|
| 194 | - $cache->cacheLoadedRelationResult($entity, $relation, $value, $this); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $entities; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Build model dictionary keyed by the relation's foreign key. |
|
| 203 | - * |
|
| 204 | - * @param EntityCollection $results |
|
| 205 | - * @return array |
|
| 206 | - */ |
|
| 207 | - protected function buildDictionary(EntityCollection $results) |
|
| 208 | - { |
|
| 209 | - $dictionary = []; |
|
| 210 | - |
|
| 211 | - $foreign = $this->firstKey; |
|
| 212 | - |
|
| 213 | - // First we will create a dictionary of entities keyed by the foreign key of the |
|
| 214 | - // relationship as this will allow us to quickly access all of the related |
|
| 215 | - // entities without having to do nested looping which will be quite slow. |
|
| 216 | - foreach ($results as $result) { |
|
| 217 | - $dictionary[$result->{$foreign}][] = $result; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return $dictionary; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Get the results of the relationship. |
|
| 225 | - * |
|
| 226 | - * @param $relation |
|
| 227 | - * @return EntityCollection |
|
| 228 | - */ |
|
| 229 | - public function getResults($relation) |
|
| 230 | - { |
|
| 231 | - $results = $this->query->get(); |
|
| 232 | - |
|
| 233 | - $this->cacheRelation($results, $relation); |
|
| 234 | - |
|
| 235 | - return $results; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Execute the query as a "select" statement. |
|
| 240 | - * |
|
| 241 | - * @param array $columns |
|
| 242 | - * @return EntityCollection |
|
| 243 | - */ |
|
| 244 | - public function get($columns = ['*']) |
|
| 245 | - { |
|
| 246 | - // First we'll add the proper select columns onto the query so it is run with |
|
| 247 | - // the proper columns. Then, we will get the results and hydrate out pivot |
|
| 248 | - // entities with the result of those columns as a separate model relation. |
|
| 249 | - $select = $this->getSelectColumns($columns); |
|
| 250 | - |
|
| 251 | - $entities = $this->query->addSelect($select)->getEntities(); |
|
| 252 | - |
|
| 253 | - // If we actually found entities we will also eager load any relationships that |
|
| 254 | - // have been specified as needing to be eager loaded. This will solve the |
|
| 255 | - // n + 1 query problem for the developer and also increase performance. |
|
| 256 | - if (count($entities) > 0) { |
|
| 257 | - $entities = $this->query->eagerLoadRelations($entities); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return $this->relatedMap->newCollection($entities); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set the select clause for the relation query. |
|
| 265 | - * |
|
| 266 | - * @param array $columns |
|
| 267 | - * @return BelongsToMany |
|
| 268 | - */ |
|
| 269 | - protected function getSelectColumns(array $columns = ['*']) |
|
| 270 | - { |
|
| 271 | - if ($columns == ['*']) { |
|
| 272 | - $columns = [$this->relatedMap->getTable() . '.*']; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - return array_merge($columns, [$this->parentMap->getTable() . '.' . $this->firstKey]); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get a paginator for the "select" statement. |
|
| 280 | - * |
|
| 281 | - * @param int $perPage |
|
| 282 | - * @param array $columns |
|
| 283 | - * @return \Illuminate\Pagination\LengthAwarePaginator |
|
| 284 | - */ |
|
| 285 | - public function paginate($perPage = null, $columns = ['*']) |
|
| 286 | - { |
|
| 287 | - $this->query->addSelect($this->getSelectColumns($columns)); |
|
| 288 | - |
|
| 289 | - return $this->query->paginate($perPage, $columns); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Get the key name of the parent model. |
|
| 294 | - * |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - protected function getQualifiedParentKeyName() |
|
| 298 | - { |
|
| 299 | - return $this->parentMap->getQualifiedKeyName(); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Get the key for comparing against the parent key in "has" query. |
|
| 304 | - * |
|
| 305 | - * @return string |
|
| 306 | - */ |
|
| 307 | - public function getHasCompareKey() |
|
| 308 | - { |
|
| 309 | - return $this->farParentMap->getQualifiedKeyName(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Run synchronization content if needed by the |
|
| 314 | - * relation type. |
|
| 315 | - * |
|
| 316 | - * @param array $actualContent |
|
| 317 | - * @return void |
|
| 318 | - */ |
|
| 319 | - public function sync(array $entities) |
|
| 320 | - { |
|
| 321 | - // N/A |
|
| 322 | - } |
|
| 12 | + /** |
|
| 13 | + * The distance parent Entity instance. |
|
| 14 | + * |
|
| 15 | + * @var \Analogue\ORM\Entity |
|
| 16 | + */ |
|
| 17 | + protected $farParent; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The far parent map instance |
|
| 21 | + * |
|
| 22 | + * @var \Analogue\ORM\EntityMap |
|
| 23 | + */ |
|
| 24 | + protected $farParentMap; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The near key on the relationship. |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + protected $firstKey; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The far key on the relationship. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $secondKey; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Create a new has many relationship instance. |
|
| 42 | + * |
|
| 43 | + * @param Mapper $mapper |
|
| 44 | + * @param \Analogue\ORM\Mappable $farParent |
|
| 45 | + * @param \Analogue\ORM\EntityMap $parentMap |
|
| 46 | + * @param string $firstKey |
|
| 47 | + * @param string $secondKey |
|
| 48 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 49 | + */ |
|
| 50 | + public function __construct(Mapper $mapper, $farParent, $parentMap, $firstKey, $secondKey) |
|
| 51 | + { |
|
| 52 | + $this->firstKey = $firstKey; |
|
| 53 | + $this->secondKey = $secondKey; |
|
| 54 | + $this->farParent = $farParent; |
|
| 55 | + |
|
| 56 | + $this->farParentMap = $this->relatedMapper->getManager()->mapper($farParent)->getEntityMap(); |
|
| 57 | + |
|
| 58 | + $parentInstance = $this->relatedMapper->getManager()->mapper($parentMap->getClass())->newInstance(); |
|
| 59 | + |
|
| 60 | + parent::__construct($mapper, $parentInstance); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param $related |
|
| 65 | + * @return mixed |
|
| 66 | + */ |
|
| 67 | + public function attachTo($related) |
|
| 68 | + { |
|
| 69 | + // N/A |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param $related |
|
| 74 | + * @return mixed |
|
| 75 | + */ |
|
| 76 | + public function detachFrom($related) |
|
| 77 | + { |
|
| 78 | + // N/A |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Set the base constraints on the relation query. |
|
| 83 | + * |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + public function addConstraints() |
|
| 87 | + { |
|
| 88 | + $parentTable = $this->parentMap->getTable(); |
|
| 89 | + |
|
| 90 | + $this->setJoin(); |
|
| 91 | + |
|
| 92 | + if (static::$constraints) { |
|
| 93 | + $farParentKeyName = $this->farParentMap->getKeyName(); |
|
| 94 | + |
|
| 95 | + $this->query->where( |
|
| 96 | + $parentTable . '.' . $this->firstKey, |
|
| 97 | + '=', |
|
| 98 | + $this->farParent->getEntityAttribute($farParentKeyName) |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Add the constraints for a relationship count query. |
|
| 105 | + * |
|
| 106 | + * @param Query $query |
|
| 107 | + * @param Query $parent |
|
| 108 | + * @return Query |
|
| 109 | + */ |
|
| 110 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 111 | + { |
|
| 112 | + $parentTable = $this->parentMap->getTable(); |
|
| 113 | + |
|
| 114 | + $this->setJoin($query); |
|
| 115 | + |
|
| 116 | + $query->select(new Expression('count(*)')); |
|
| 117 | + |
|
| 118 | + $key = $this->wrap($parentTable . '.' . $this->firstKey); |
|
| 119 | + |
|
| 120 | + return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Set the join clause on the query. |
|
| 125 | + * |
|
| 126 | + * @param null|Query $query |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + protected function setJoin(Query $query = null) |
|
| 130 | + { |
|
| 131 | + $query = $query ?: $this->query; |
|
| 132 | + |
|
| 133 | + $foreignKey = $this->relatedMap->getTable() . '.' . $this->secondKey; |
|
| 134 | + |
|
| 135 | + $query->join($this->parentMap->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Set the constraints for an eager load of the relation. |
|
| 140 | + * |
|
| 141 | + * @param array $entities |
|
| 142 | + * @return void |
|
| 143 | + */ |
|
| 144 | + public function addEagerConstraints(array $entities) |
|
| 145 | + { |
|
| 146 | + $table = $this->parentMap->getTable(); |
|
| 147 | + |
|
| 148 | + $this->query->whereIn($table . '.' . $this->firstKey, $this->getKeys($entities)); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Initialize the relation on a set of entities. |
|
| 153 | + * |
|
| 154 | + * @param \Analogue\ORM\Entity[] $entities |
|
| 155 | + * @param string $relation |
|
| 156 | + * @return \Analogue\ORM\Entity[] |
|
| 157 | + */ |
|
| 158 | + public function initRelation(array $entities, $relation) |
|
| 159 | + { |
|
| 160 | + foreach ($entities as $entity) { |
|
| 161 | + $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return $entities; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Match the eagerly loaded results to their parents. |
|
| 169 | + * |
|
| 170 | + * @param \Analogue\ORM\Entity[] $entities |
|
| 171 | + * @param EntityCollection $results |
|
| 172 | + * @param string $relation |
|
| 173 | + * @return \Analogue\ORM\Entity[] |
|
| 174 | + */ |
|
| 175 | + public function match(array $entities, EntityCollection $results, $relation) |
|
| 176 | + { |
|
| 177 | + $dictionary = $this->buildDictionary($results); |
|
| 178 | + |
|
| 179 | + $relatedKey = $this->relatedMap->getKeyName(); |
|
| 180 | + |
|
| 181 | + $cache = $this->parentMapper->getEntityCache(); |
|
| 182 | + |
|
| 183 | + // Once we have the dictionary we can simply spin through the parent entities to |
|
| 184 | + // link them up with their children using the keyed dictionary to make the |
|
| 185 | + // matching very convenient and easy work. Then we'll just return them. |
|
| 186 | + foreach ($entities as $entity) { |
|
| 187 | + $key = $entity->getEntityAttribute($relatedKey); |
|
| 188 | + |
|
| 189 | + if (isset($dictionary[$key])) { |
|
| 190 | + $value = $this->relatedMap->newCollection($dictionary[$key]); |
|
| 191 | + |
|
| 192 | + $entity->setEntityAttribute($relation, $value); |
|
| 193 | + |
|
| 194 | + $cache->cacheLoadedRelationResult($entity, $relation, $value, $this); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $entities; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Build model dictionary keyed by the relation's foreign key. |
|
| 203 | + * |
|
| 204 | + * @param EntityCollection $results |
|
| 205 | + * @return array |
|
| 206 | + */ |
|
| 207 | + protected function buildDictionary(EntityCollection $results) |
|
| 208 | + { |
|
| 209 | + $dictionary = []; |
|
| 210 | + |
|
| 211 | + $foreign = $this->firstKey; |
|
| 212 | + |
|
| 213 | + // First we will create a dictionary of entities keyed by the foreign key of the |
|
| 214 | + // relationship as this will allow us to quickly access all of the related |
|
| 215 | + // entities without having to do nested looping which will be quite slow. |
|
| 216 | + foreach ($results as $result) { |
|
| 217 | + $dictionary[$result->{$foreign}][] = $result; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return $dictionary; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Get the results of the relationship. |
|
| 225 | + * |
|
| 226 | + * @param $relation |
|
| 227 | + * @return EntityCollection |
|
| 228 | + */ |
|
| 229 | + public function getResults($relation) |
|
| 230 | + { |
|
| 231 | + $results = $this->query->get(); |
|
| 232 | + |
|
| 233 | + $this->cacheRelation($results, $relation); |
|
| 234 | + |
|
| 235 | + return $results; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Execute the query as a "select" statement. |
|
| 240 | + * |
|
| 241 | + * @param array $columns |
|
| 242 | + * @return EntityCollection |
|
| 243 | + */ |
|
| 244 | + public function get($columns = ['*']) |
|
| 245 | + { |
|
| 246 | + // First we'll add the proper select columns onto the query so it is run with |
|
| 247 | + // the proper columns. Then, we will get the results and hydrate out pivot |
|
| 248 | + // entities with the result of those columns as a separate model relation. |
|
| 249 | + $select = $this->getSelectColumns($columns); |
|
| 250 | + |
|
| 251 | + $entities = $this->query->addSelect($select)->getEntities(); |
|
| 252 | + |
|
| 253 | + // If we actually found entities we will also eager load any relationships that |
|
| 254 | + // have been specified as needing to be eager loaded. This will solve the |
|
| 255 | + // n + 1 query problem for the developer and also increase performance. |
|
| 256 | + if (count($entities) > 0) { |
|
| 257 | + $entities = $this->query->eagerLoadRelations($entities); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return $this->relatedMap->newCollection($entities); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set the select clause for the relation query. |
|
| 265 | + * |
|
| 266 | + * @param array $columns |
|
| 267 | + * @return BelongsToMany |
|
| 268 | + */ |
|
| 269 | + protected function getSelectColumns(array $columns = ['*']) |
|
| 270 | + { |
|
| 271 | + if ($columns == ['*']) { |
|
| 272 | + $columns = [$this->relatedMap->getTable() . '.*']; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + return array_merge($columns, [$this->parentMap->getTable() . '.' . $this->firstKey]); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get a paginator for the "select" statement. |
|
| 280 | + * |
|
| 281 | + * @param int $perPage |
|
| 282 | + * @param array $columns |
|
| 283 | + * @return \Illuminate\Pagination\LengthAwarePaginator |
|
| 284 | + */ |
|
| 285 | + public function paginate($perPage = null, $columns = ['*']) |
|
| 286 | + { |
|
| 287 | + $this->query->addSelect($this->getSelectColumns($columns)); |
|
| 288 | + |
|
| 289 | + return $this->query->paginate($perPage, $columns); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Get the key name of the parent model. |
|
| 294 | + * |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + protected function getQualifiedParentKeyName() |
|
| 298 | + { |
|
| 299 | + return $this->parentMap->getQualifiedKeyName(); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Get the key for comparing against the parent key in "has" query. |
|
| 304 | + * |
|
| 305 | + * @return string |
|
| 306 | + */ |
|
| 307 | + public function getHasCompareKey() |
|
| 308 | + { |
|
| 309 | + return $this->farParentMap->getQualifiedKeyName(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Run synchronization content if needed by the |
|
| 314 | + * relation type. |
|
| 315 | + * |
|
| 316 | + * @param array $actualContent |
|
| 317 | + * @return void |
|
| 318 | + */ |
|
| 319 | + public function sync(array $entities) |
|
| 320 | + { |
|
| 321 | + // N/A |
|
| 322 | + } |
|
| 323 | 323 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | |
| 258 | 258 | $host = $this; |
| 259 | 259 | |
| 260 | - return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
| 260 | + return array_unique(array_values(array_map(function($value) use ($key, $host) { |
|
| 261 | 261 | if (!$value instanceof InternallyMappable) { |
| 262 | 262 | $value = $host->factory->make($value); |
| 263 | 263 | } |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | |
| 417 | 417 | $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); |
| 418 | 418 | |
| 419 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 419 | + return $class.'.'.$entity->getEntityAttribute($keyName); |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | /** |
@@ -19,430 +19,430 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | abstract class Relationship |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * The mapper instance for the related entity |
|
| 24 | - * |
|
| 25 | - * @var Mapper |
|
| 26 | - */ |
|
| 27 | - protected $relatedMapper; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The Analogue Query Builder instance. |
|
| 31 | - * |
|
| 32 | - * @var Query |
|
| 33 | - */ |
|
| 34 | - protected $query; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The parent entity proxy instance. |
|
| 38 | - * |
|
| 39 | - * @var InternallyMappable |
|
| 40 | - */ |
|
| 41 | - protected $parent; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The parent entity map |
|
| 45 | - * @var \Analogue\ORM\EntityMap |
|
| 46 | - */ |
|
| 47 | - protected $parentMap; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The Parent Mapper instance |
|
| 51 | - * |
|
| 52 | - * @var Mapper |
|
| 53 | - */ |
|
| 54 | - protected $parentMapper; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The related entity instance. |
|
| 58 | - * |
|
| 59 | - * @var object |
|
| 60 | - */ |
|
| 61 | - protected $related; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The related entity Map |
|
| 65 | - * @var \Analogue\ORM\EntityMap |
|
| 66 | - */ |
|
| 67 | - protected $relatedMap; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Indicate if the parent entity hold the key for the relation. |
|
| 71 | - * |
|
| 72 | - * @var boolean |
|
| 73 | - */ |
|
| 74 | - protected static $ownForeignKey = false; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Indicate if the relationships use a pivot table.* |
|
| 78 | - * |
|
| 79 | - * @var boolean |
|
| 80 | - */ |
|
| 81 | - protected static $hasPivot = false; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Indicates if the relation is adding constraints. |
|
| 85 | - * |
|
| 86 | - * @var bool |
|
| 87 | - */ |
|
| 88 | - protected static $constraints = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Wrapper factory |
|
| 92 | - * |
|
| 93 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 94 | - */ |
|
| 95 | - protected $factory; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Create a new relation instance. |
|
| 99 | - * |
|
| 100 | - * @param Mapper $mapper |
|
| 101 | - * @param Mappable $parent |
|
| 102 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 103 | - */ |
|
| 104 | - public function __construct(Mapper $mapper, $parent) |
|
| 105 | - { |
|
| 106 | - $this->relatedMapper = $mapper; |
|
| 107 | - |
|
| 108 | - $this->query = $mapper->getQuery(); |
|
| 109 | - |
|
| 110 | - $this->factory = new Factory; |
|
| 111 | - |
|
| 112 | - $this->parent = $this->factory->make($parent); |
|
| 113 | - |
|
| 114 | - $this->parentMapper = $mapper->getManager()->getMapper($parent); |
|
| 115 | - |
|
| 116 | - $this->parentMap = $this->parentMapper->getEntityMap(); |
|
| 117 | - |
|
| 118 | - $this->related = $this->query->getEntityInstance(); |
|
| 119 | - |
|
| 120 | - $this->relatedMap = $mapper->getEntityMap(); |
|
| 121 | - |
|
| 122 | - $this->addConstraints(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param $related |
|
| 127 | - * @return mixed |
|
| 128 | - */ |
|
| 129 | - abstract public function attachTo($related); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param $related |
|
| 133 | - * @return mixed |
|
| 134 | - */ |
|
| 135 | - abstract public function detachFrom($related); |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Indicate if the parent entity hold the foreign key for relation. |
|
| 139 | - * |
|
| 140 | - * @return boolean |
|
| 141 | - */ |
|
| 142 | - public function ownForeignKey() |
|
| 143 | - { |
|
| 144 | - return static::$ownForeignKey; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Indicate if the relationship uses a pivot table |
|
| 149 | - * |
|
| 150 | - * @return boolean |
|
| 151 | - */ |
|
| 152 | - public function hasPivot() |
|
| 153 | - { |
|
| 154 | - return static::$hasPivot; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Set the base constraints on the relation query. |
|
| 159 | - * |
|
| 160 | - * @return void |
|
| 161 | - */ |
|
| 162 | - abstract public function addConstraints(); |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Set the constraints for an eager load of the relation. |
|
| 166 | - * |
|
| 167 | - * @param array $models |
|
| 168 | - * @return void |
|
| 169 | - */ |
|
| 170 | - abstract public function addEagerConstraints(array $models); |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Initialize the relation on a set of models. |
|
| 174 | - * |
|
| 175 | - * @param array $models |
|
| 176 | - * @param string $relation |
|
| 177 | - * @return array |
|
| 178 | - */ |
|
| 179 | - abstract public function initRelation(array $models, $relation); |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Match the eagerly loaded results to their parents. |
|
| 183 | - * |
|
| 184 | - * @param array $entities |
|
| 185 | - * @param EntityCollection $results |
|
| 186 | - * @param string $relation |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - abstract public function match(array $entities, EntityCollection $results, $relation); |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get the results of the relationship. |
|
| 193 | - * |
|
| 194 | - * @param string $relation relation name in parent's entity map |
|
| 195 | - * @return mixed |
|
| 196 | - */ |
|
| 197 | - abstract public function getResults($relation); |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get the relationship for eager loading. |
|
| 201 | - * |
|
| 202 | - * @return EntityCollection |
|
| 203 | - */ |
|
| 204 | - public function getEager() |
|
| 205 | - { |
|
| 206 | - return $this->get(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Add the constraints for a relationship count query. |
|
| 211 | - * |
|
| 212 | - * @param Query $query |
|
| 213 | - * @param Query $parent |
|
| 214 | - * @return Query |
|
| 215 | - */ |
|
| 216 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 217 | - { |
|
| 218 | - $query->select(new Expression('count(*)')); |
|
| 219 | - |
|
| 220 | - $key = $this->wrap($this->getQualifiedParentKeyName()); |
|
| 221 | - |
|
| 222 | - return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Run a callback with constraints disabled on the relation. |
|
| 227 | - * |
|
| 228 | - * @param Closure $callback |
|
| 229 | - * @return mixed |
|
| 230 | - */ |
|
| 231 | - public static function noConstraints(Closure $callback) |
|
| 232 | - { |
|
| 233 | - static::$constraints = false; |
|
| 234 | - |
|
| 235 | - // When resetting the relation where clause, we want to shift the first element |
|
| 236 | - // off of the bindings, leaving only the constraints that the developers put |
|
| 237 | - // as "extra" on the relationships, and not original relation constraints. |
|
| 238 | - $results = call_user_func($callback); |
|
| 239 | - |
|
| 240 | - static::$constraints = true; |
|
| 241 | - |
|
| 242 | - return $results; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get all of the primary keys for an array of entities. |
|
| 247 | - * |
|
| 248 | - * @param array $entities |
|
| 249 | - * @param string $key |
|
| 250 | - * @return array |
|
| 251 | - */ |
|
| 252 | - protected function getKeys(array $entities, $key = null) |
|
| 253 | - { |
|
| 254 | - if (is_null($key)) { |
|
| 255 | - $key = $this->relatedMap->getKeyName(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $host = $this; |
|
| 259 | - |
|
| 260 | - return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
| 261 | - if (!$value instanceof InternallyMappable) { |
|
| 262 | - $value = $host->factory->make($value); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $value->getEntityAttribute($key); |
|
| 266 | - |
|
| 267 | - }, $entities))); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Get the underlying query for the relation. |
|
| 272 | - * |
|
| 273 | - * @return Query |
|
| 274 | - */ |
|
| 275 | - public function getQuery() |
|
| 276 | - { |
|
| 277 | - return $this->query; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Get the base query builder |
|
| 282 | - * |
|
| 283 | - * @return \Analogue\ORM\Drivers\QueryAdapter |
|
| 284 | - */ |
|
| 285 | - public function getBaseQuery() |
|
| 286 | - { |
|
| 287 | - return $this->query->getQuery(); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Get the parent model of the relation. |
|
| 292 | - * |
|
| 293 | - * @return InternallyMappable |
|
| 294 | - */ |
|
| 295 | - public function getParent() |
|
| 296 | - { |
|
| 297 | - return $this->parent; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Get the fully qualified parent key name. |
|
| 302 | - * |
|
| 303 | - * @return string |
|
| 304 | - */ |
|
| 305 | - protected function getQualifiedParentKeyName() |
|
| 306 | - { |
|
| 307 | - return $this->parent->getQualifiedKeyName(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get the related entity of the relation. |
|
| 312 | - * |
|
| 313 | - * @return \Analogue\ORM\Entity |
|
| 314 | - */ |
|
| 315 | - public function getRelated() |
|
| 316 | - { |
|
| 317 | - return $this->related; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the related mapper for the relation |
|
| 322 | - * |
|
| 323 | - * @return Mapper |
|
| 324 | - */ |
|
| 325 | - public function getRelatedMapper() |
|
| 326 | - { |
|
| 327 | - return $this->relatedMapper; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Get the name of the "created at" column. |
|
| 333 | - * |
|
| 334 | - * @return string |
|
| 335 | - */ |
|
| 336 | - public function createdAt() |
|
| 337 | - { |
|
| 338 | - return $this->parentMap->getCreatedAtColumn(); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get the name of the "updated at" column. |
|
| 343 | - * |
|
| 344 | - * @return string |
|
| 345 | - */ |
|
| 346 | - public function updatedAt() |
|
| 347 | - { |
|
| 348 | - return $this->parentMap->getUpdatedAtColumn(); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Get the name of the related model's "updated at" column. |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public function relatedUpdatedAt() |
|
| 357 | - { |
|
| 358 | - return $this->related->getUpdatedAtColumn(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Wrap the given value with the parent query's grammar. |
|
| 363 | - * |
|
| 364 | - * @param string $value |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - public function wrap($value) |
|
| 368 | - { |
|
| 369 | - return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Get a fresh timestamp |
|
| 374 | - * |
|
| 375 | - * @return Carbon |
|
| 376 | - */ |
|
| 377 | - protected function freshTimestamp() |
|
| 378 | - { |
|
| 379 | - return new Carbon; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Cache the link between parent and related |
|
| 384 | - * into the mapper's Entity Cache. |
|
| 385 | - * |
|
| 386 | - * @param EntityCollection|Mappable $results result of the relation query |
|
| 387 | - * @param string $relation name of the relation method on the parent entity |
|
| 388 | - * @return void |
|
| 389 | - */ |
|
| 390 | - protected function cacheRelation($results, $relation) |
|
| 391 | - { |
|
| 392 | - $cache = $this->parentMapper->getEntityCache(); |
|
| 393 | - |
|
| 394 | - $cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Return Pivot attributes when available on a relationship |
|
| 399 | - * |
|
| 400 | - * @return array |
|
| 401 | - */ |
|
| 402 | - public function getPivotAttributes() |
|
| 403 | - { |
|
| 404 | - return []; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Get a combo type.primaryKey |
|
| 409 | - * |
|
| 410 | - * @param Mappable $entity |
|
| 411 | - * @return string |
|
| 412 | - */ |
|
| 413 | - protected function getEntityHash(Mappable $entity) |
|
| 414 | - { |
|
| 415 | - $class = get_class($entity); |
|
| 416 | - |
|
| 417 | - $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); |
|
| 418 | - |
|
| 419 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Run synchronization content if needed by the |
|
| 424 | - * relation type. |
|
| 425 | - * |
|
| 426 | - * @param array $actualContent |
|
| 427 | - * @return void |
|
| 428 | - */ |
|
| 429 | - abstract public function sync(array $actualContent); |
|
| 22 | + /** |
|
| 23 | + * The mapper instance for the related entity |
|
| 24 | + * |
|
| 25 | + * @var Mapper |
|
| 26 | + */ |
|
| 27 | + protected $relatedMapper; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The Analogue Query Builder instance. |
|
| 31 | + * |
|
| 32 | + * @var Query |
|
| 33 | + */ |
|
| 34 | + protected $query; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The parent entity proxy instance. |
|
| 38 | + * |
|
| 39 | + * @var InternallyMappable |
|
| 40 | + */ |
|
| 41 | + protected $parent; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The parent entity map |
|
| 45 | + * @var \Analogue\ORM\EntityMap |
|
| 46 | + */ |
|
| 47 | + protected $parentMap; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The Parent Mapper instance |
|
| 51 | + * |
|
| 52 | + * @var Mapper |
|
| 53 | + */ |
|
| 54 | + protected $parentMapper; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The related entity instance. |
|
| 58 | + * |
|
| 59 | + * @var object |
|
| 60 | + */ |
|
| 61 | + protected $related; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The related entity Map |
|
| 65 | + * @var \Analogue\ORM\EntityMap |
|
| 66 | + */ |
|
| 67 | + protected $relatedMap; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Indicate if the parent entity hold the key for the relation. |
|
| 71 | + * |
|
| 72 | + * @var boolean |
|
| 73 | + */ |
|
| 74 | + protected static $ownForeignKey = false; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Indicate if the relationships use a pivot table.* |
|
| 78 | + * |
|
| 79 | + * @var boolean |
|
| 80 | + */ |
|
| 81 | + protected static $hasPivot = false; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Indicates if the relation is adding constraints. |
|
| 85 | + * |
|
| 86 | + * @var bool |
|
| 87 | + */ |
|
| 88 | + protected static $constraints = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Wrapper factory |
|
| 92 | + * |
|
| 93 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 94 | + */ |
|
| 95 | + protected $factory; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Create a new relation instance. |
|
| 99 | + * |
|
| 100 | + * @param Mapper $mapper |
|
| 101 | + * @param Mappable $parent |
|
| 102 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 103 | + */ |
|
| 104 | + public function __construct(Mapper $mapper, $parent) |
|
| 105 | + { |
|
| 106 | + $this->relatedMapper = $mapper; |
|
| 107 | + |
|
| 108 | + $this->query = $mapper->getQuery(); |
|
| 109 | + |
|
| 110 | + $this->factory = new Factory; |
|
| 111 | + |
|
| 112 | + $this->parent = $this->factory->make($parent); |
|
| 113 | + |
|
| 114 | + $this->parentMapper = $mapper->getManager()->getMapper($parent); |
|
| 115 | + |
|
| 116 | + $this->parentMap = $this->parentMapper->getEntityMap(); |
|
| 117 | + |
|
| 118 | + $this->related = $this->query->getEntityInstance(); |
|
| 119 | + |
|
| 120 | + $this->relatedMap = $mapper->getEntityMap(); |
|
| 121 | + |
|
| 122 | + $this->addConstraints(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param $related |
|
| 127 | + * @return mixed |
|
| 128 | + */ |
|
| 129 | + abstract public function attachTo($related); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param $related |
|
| 133 | + * @return mixed |
|
| 134 | + */ |
|
| 135 | + abstract public function detachFrom($related); |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Indicate if the parent entity hold the foreign key for relation. |
|
| 139 | + * |
|
| 140 | + * @return boolean |
|
| 141 | + */ |
|
| 142 | + public function ownForeignKey() |
|
| 143 | + { |
|
| 144 | + return static::$ownForeignKey; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Indicate if the relationship uses a pivot table |
|
| 149 | + * |
|
| 150 | + * @return boolean |
|
| 151 | + */ |
|
| 152 | + public function hasPivot() |
|
| 153 | + { |
|
| 154 | + return static::$hasPivot; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Set the base constraints on the relation query. |
|
| 159 | + * |
|
| 160 | + * @return void |
|
| 161 | + */ |
|
| 162 | + abstract public function addConstraints(); |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Set the constraints for an eager load of the relation. |
|
| 166 | + * |
|
| 167 | + * @param array $models |
|
| 168 | + * @return void |
|
| 169 | + */ |
|
| 170 | + abstract public function addEagerConstraints(array $models); |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Initialize the relation on a set of models. |
|
| 174 | + * |
|
| 175 | + * @param array $models |
|
| 176 | + * @param string $relation |
|
| 177 | + * @return array |
|
| 178 | + */ |
|
| 179 | + abstract public function initRelation(array $models, $relation); |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Match the eagerly loaded results to their parents. |
|
| 183 | + * |
|
| 184 | + * @param array $entities |
|
| 185 | + * @param EntityCollection $results |
|
| 186 | + * @param string $relation |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + abstract public function match(array $entities, EntityCollection $results, $relation); |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get the results of the relationship. |
|
| 193 | + * |
|
| 194 | + * @param string $relation relation name in parent's entity map |
|
| 195 | + * @return mixed |
|
| 196 | + */ |
|
| 197 | + abstract public function getResults($relation); |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get the relationship for eager loading. |
|
| 201 | + * |
|
| 202 | + * @return EntityCollection |
|
| 203 | + */ |
|
| 204 | + public function getEager() |
|
| 205 | + { |
|
| 206 | + return $this->get(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Add the constraints for a relationship count query. |
|
| 211 | + * |
|
| 212 | + * @param Query $query |
|
| 213 | + * @param Query $parent |
|
| 214 | + * @return Query |
|
| 215 | + */ |
|
| 216 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 217 | + { |
|
| 218 | + $query->select(new Expression('count(*)')); |
|
| 219 | + |
|
| 220 | + $key = $this->wrap($this->getQualifiedParentKeyName()); |
|
| 221 | + |
|
| 222 | + return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Run a callback with constraints disabled on the relation. |
|
| 227 | + * |
|
| 228 | + * @param Closure $callback |
|
| 229 | + * @return mixed |
|
| 230 | + */ |
|
| 231 | + public static function noConstraints(Closure $callback) |
|
| 232 | + { |
|
| 233 | + static::$constraints = false; |
|
| 234 | + |
|
| 235 | + // When resetting the relation where clause, we want to shift the first element |
|
| 236 | + // off of the bindings, leaving only the constraints that the developers put |
|
| 237 | + // as "extra" on the relationships, and not original relation constraints. |
|
| 238 | + $results = call_user_func($callback); |
|
| 239 | + |
|
| 240 | + static::$constraints = true; |
|
| 241 | + |
|
| 242 | + return $results; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get all of the primary keys for an array of entities. |
|
| 247 | + * |
|
| 248 | + * @param array $entities |
|
| 249 | + * @param string $key |
|
| 250 | + * @return array |
|
| 251 | + */ |
|
| 252 | + protected function getKeys(array $entities, $key = null) |
|
| 253 | + { |
|
| 254 | + if (is_null($key)) { |
|
| 255 | + $key = $this->relatedMap->getKeyName(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $host = $this; |
|
| 259 | + |
|
| 260 | + return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
| 261 | + if (!$value instanceof InternallyMappable) { |
|
| 262 | + $value = $host->factory->make($value); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $value->getEntityAttribute($key); |
|
| 266 | + |
|
| 267 | + }, $entities))); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Get the underlying query for the relation. |
|
| 272 | + * |
|
| 273 | + * @return Query |
|
| 274 | + */ |
|
| 275 | + public function getQuery() |
|
| 276 | + { |
|
| 277 | + return $this->query; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Get the base query builder |
|
| 282 | + * |
|
| 283 | + * @return \Analogue\ORM\Drivers\QueryAdapter |
|
| 284 | + */ |
|
| 285 | + public function getBaseQuery() |
|
| 286 | + { |
|
| 287 | + return $this->query->getQuery(); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Get the parent model of the relation. |
|
| 292 | + * |
|
| 293 | + * @return InternallyMappable |
|
| 294 | + */ |
|
| 295 | + public function getParent() |
|
| 296 | + { |
|
| 297 | + return $this->parent; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Get the fully qualified parent key name. |
|
| 302 | + * |
|
| 303 | + * @return string |
|
| 304 | + */ |
|
| 305 | + protected function getQualifiedParentKeyName() |
|
| 306 | + { |
|
| 307 | + return $this->parent->getQualifiedKeyName(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get the related entity of the relation. |
|
| 312 | + * |
|
| 313 | + * @return \Analogue\ORM\Entity |
|
| 314 | + */ |
|
| 315 | + public function getRelated() |
|
| 316 | + { |
|
| 317 | + return $this->related; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the related mapper for the relation |
|
| 322 | + * |
|
| 323 | + * @return Mapper |
|
| 324 | + */ |
|
| 325 | + public function getRelatedMapper() |
|
| 326 | + { |
|
| 327 | + return $this->relatedMapper; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Get the name of the "created at" column. |
|
| 333 | + * |
|
| 334 | + * @return string |
|
| 335 | + */ |
|
| 336 | + public function createdAt() |
|
| 337 | + { |
|
| 338 | + return $this->parentMap->getCreatedAtColumn(); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get the name of the "updated at" column. |
|
| 343 | + * |
|
| 344 | + * @return string |
|
| 345 | + */ |
|
| 346 | + public function updatedAt() |
|
| 347 | + { |
|
| 348 | + return $this->parentMap->getUpdatedAtColumn(); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Get the name of the related model's "updated at" column. |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public function relatedUpdatedAt() |
|
| 357 | + { |
|
| 358 | + return $this->related->getUpdatedAtColumn(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Wrap the given value with the parent query's grammar. |
|
| 363 | + * |
|
| 364 | + * @param string $value |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + public function wrap($value) |
|
| 368 | + { |
|
| 369 | + return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Get a fresh timestamp |
|
| 374 | + * |
|
| 375 | + * @return Carbon |
|
| 376 | + */ |
|
| 377 | + protected function freshTimestamp() |
|
| 378 | + { |
|
| 379 | + return new Carbon; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Cache the link between parent and related |
|
| 384 | + * into the mapper's Entity Cache. |
|
| 385 | + * |
|
| 386 | + * @param EntityCollection|Mappable $results result of the relation query |
|
| 387 | + * @param string $relation name of the relation method on the parent entity |
|
| 388 | + * @return void |
|
| 389 | + */ |
|
| 390 | + protected function cacheRelation($results, $relation) |
|
| 391 | + { |
|
| 392 | + $cache = $this->parentMapper->getEntityCache(); |
|
| 393 | + |
|
| 394 | + $cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Return Pivot attributes when available on a relationship |
|
| 399 | + * |
|
| 400 | + * @return array |
|
| 401 | + */ |
|
| 402 | + public function getPivotAttributes() |
|
| 403 | + { |
|
| 404 | + return []; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Get a combo type.primaryKey |
|
| 409 | + * |
|
| 410 | + * @param Mappable $entity |
|
| 411 | + * @return string |
|
| 412 | + */ |
|
| 413 | + protected function getEntityHash(Mappable $entity) |
|
| 414 | + { |
|
| 415 | + $class = get_class($entity); |
|
| 416 | + |
|
| 417 | + $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); |
|
| 418 | + |
|
| 419 | + return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Run synchronization content if needed by the |
|
| 424 | + * relation type. |
|
| 425 | + * |
|
| 426 | + * @param array $actualContent |
|
| 427 | + * @return void |
|
| 428 | + */ |
|
| 429 | + abstract public function sync(array $actualContent); |
|
| 430 | 430 | |
| 431 | - /** |
|
| 432 | - * Handle dynamic method calls to the relationship. |
|
| 433 | - * |
|
| 434 | - * @param string $method |
|
| 435 | - * @param array $parameters |
|
| 436 | - * @return mixed |
|
| 437 | - */ |
|
| 438 | - public function __call($method, $parameters) |
|
| 439 | - { |
|
| 440 | - $result = call_user_func_array([$this->query, $method], $parameters); |
|
| 441 | - |
|
| 442 | - if ($result === $this->query) { |
|
| 443 | - return $this; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - return $result; |
|
| 447 | - } |
|
| 431 | + /** |
|
| 432 | + * Handle dynamic method calls to the relationship. |
|
| 433 | + * |
|
| 434 | + * @param string $method |
|
| 435 | + * @param array $parameters |
|
| 436 | + * @return mixed |
|
| 437 | + */ |
|
| 438 | + public function __call($method, $parameters) |
|
| 439 | + { |
|
| 440 | + $result = call_user_func_array([$this->query, $method], $parameters); |
|
| 441 | + |
|
| 442 | + if ($result === $this->query) { |
|
| 443 | + return $this; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + return $result; |
|
| 447 | + } |
|
| 448 | 448 | } |
@@ -6,38 +6,38 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Delete extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Execute the Delete Statement |
|
| 11 | - * |
|
| 12 | - * @throws MappingException |
|
| 13 | - * @throws \InvalidArgumentException |
|
| 14 | - * @return false|void |
|
| 15 | - */ |
|
| 16 | - public function execute() |
|
| 17 | - { |
|
| 18 | - $aggregate = $this->aggregate; |
|
| 19 | - |
|
| 20 | - $entity = $aggregate->getEntityObject(); |
|
| 21 | - |
|
| 22 | - $mapper = $aggregate->getMapper(); |
|
| 23 | - |
|
| 24 | - if ($mapper->fireEvent('deleting', $entity) === false) { |
|
| 25 | - return false; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 9 | + /** |
|
| 10 | + * Execute the Delete Statement |
|
| 11 | + * |
|
| 12 | + * @throws MappingException |
|
| 13 | + * @throws \InvalidArgumentException |
|
| 14 | + * @return false|void |
|
| 15 | + */ |
|
| 16 | + public function execute() |
|
| 17 | + { |
|
| 18 | + $aggregate = $this->aggregate; |
|
| 19 | + |
|
| 20 | + $entity = $aggregate->getEntityObject(); |
|
| 21 | + |
|
| 22 | + $mapper = $aggregate->getMapper(); |
|
| 23 | + |
|
| 24 | + if ($mapper->fireEvent('deleting', $entity) === false) { |
|
| 25 | + return false; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 29 | 29 | |
| 30 | - $id = $this->aggregate->getEntityId(); |
|
| 30 | + $id = $this->aggregate->getEntityId(); |
|
| 31 | 31 | |
| 32 | - if (is_null($id)) { |
|
| 33 | - throw new MappingException('Executed a delete command on an entity with "null" as primary key'); |
|
| 34 | - } |
|
| 32 | + if (is_null($id)) { |
|
| 33 | + throw new MappingException('Executed a delete command on an entity with "null" as primary key'); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - $this->query->where($keyName, '=', $id)->delete(); |
|
| 36 | + $this->query->where($keyName, '=', $id)->delete(); |
|
| 37 | 37 | |
| 38 | - $mapper->fireEvent('deleted', $entity, false); |
|
| 38 | + $mapper->fireEvent('deleted', $entity, false); |
|
| 39 | 39 | |
| 40 | - // Once the Entity is successfully deleted, we'll just set the primary key to null. |
|
| 41 | - $aggregate->setEntityAttribute($keyName, null); |
|
| 42 | - } |
|
| 40 | + // Once the Entity is successfully deleted, we'll just set the primary key to null. |
|
| 41 | + $aggregate->setEntityAttribute($keyName, null); |
|
| 42 | + } |
|
| 43 | 43 | } |