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