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