1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm\Mapper\Relation; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Utils\Text; |
13
|
|
|
use Slick\Database\Sql; |
14
|
|
|
use Slick\Orm\Entity\EntityCollection; |
15
|
|
|
use Slick\Orm\Entity\EntityCollectionInterface; |
16
|
|
|
use Slick\Orm\EntityInterface; |
17
|
|
|
use Slick\Orm\Event\EntityAdded; |
18
|
|
|
use Slick\Orm\Mapper\RelationInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HasMany |
22
|
|
|
* |
23
|
|
|
* @package Slick\Orm\Mapper\Relation |
24
|
|
|
* @author Filipe Silva <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class HasMany extends AbstractRelation implements RelationInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Relations utility methods |
30
|
|
|
*/ |
31
|
|
|
use RelationsUtilityMethods; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @readwrite |
35
|
|
|
* @var integer |
36
|
|
|
*/ |
37
|
|
|
protected $limit; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @readwrite |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $order; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @readwrite |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
protected $conditions; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* BelongsTo relation |
53
|
|
|
* |
54
|
|
|
* @param array|object $options The parameters from annotation |
55
|
|
|
*/ |
56
|
22 |
|
public function __construct($options) |
57
|
|
|
{ |
58
|
|
|
/** @var \Slick\Orm\Annotations\BelongsTo $annotation */ |
59
|
22 |
|
$annotation = $options['annotation']; |
60
|
22 |
|
unset($options['annotation']); |
61
|
22 |
|
$options['foreignKey'] = $annotation->getParameter('foreignKey'); |
62
|
22 |
|
$options['parentEntity'] = $annotation->getValue(); |
63
|
22 |
|
$options['limit'] = $annotation->getParameter('limit'); |
64
|
22 |
|
$options['order'] = $annotation->getParameter('order'); |
65
|
22 |
|
$options['conditions'] = $annotation->getParameter('conditions'); |
66
|
|
|
|
67
|
22 |
|
parent::__construct($options); |
68
|
22 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Loads the entity or entity collection for this relation |
72
|
|
|
* |
73
|
|
|
* @param EntityInterface $entity |
74
|
|
|
* |
75
|
|
|
* @return null|EntityInterface|EntityCollection|EntityInterface[] |
76
|
|
|
*/ |
77
|
2 |
|
public function load(EntityInterface $entity) |
78
|
|
|
{ |
79
|
2 |
|
$repository = $this->getParentRepository(); |
80
|
|
|
|
81
|
2 |
|
$query = $repository->find() |
82
|
2 |
|
->where($this->getConditions($entity)) |
|
|
|
|
83
|
2 |
|
->limit($this->limit); |
84
|
2 |
|
$this->checkConditions($query) |
85
|
2 |
|
->checkOrder($query); |
86
|
|
|
|
87
|
|
|
/** @var EntityCollectionInterface $collection */ |
88
|
2 |
|
$collection = $query->all(); |
89
|
|
|
|
90
|
|
|
$collection |
91
|
2 |
|
->setParentEntity($entity) |
92
|
2 |
|
->getEmitter() |
93
|
2 |
|
->addListener(EntityAdded::ACTION_ADD, [$this, 'add']); |
94
|
|
|
|
95
|
2 |
|
return $collection; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Saves the relation foreign key upon entity add |
100
|
|
|
* |
101
|
|
|
* @param EntityAdded $event |
102
|
|
|
*/ |
103
|
2 |
|
public function add(EntityAdded $event) |
104
|
|
|
{ |
105
|
2 |
|
$entity = $event->getEntity(); |
106
|
2 |
|
$table = $this->getParentTableName(); |
107
|
2 |
|
$pmk = $this->getParentPrimaryKey(); |
108
|
2 |
|
$value = $event->getCollection()->parentEntity()->getId(); |
109
|
2 |
|
Sql::createSql($this->getAdapter()) |
110
|
2 |
|
->update($table) |
111
|
2 |
|
->set([$this->getForeignKey() => $value]) |
112
|
2 |
|
->where(["{$pmk} = :id" => [':id' => $entity->getId()]]) |
|
|
|
|
113
|
2 |
|
->execute(); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the relation conditions |
118
|
|
|
* |
119
|
|
|
* @param EntityInterface $entity |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
2 |
|
protected function getConditions(EntityInterface $entity) |
123
|
|
|
{ |
124
|
2 |
|
$field = "{$this->getParentTableName()}.{$this->getForeignKey()}"; |
125
|
2 |
|
$property = $this->getPropertyName(); |
126
|
|
|
$conditions = [ |
127
|
2 |
|
"{$field} = :{$property}" => [ |
128
|
2 |
|
":{$property}" => $entity->getId() |
129
|
2 |
|
] |
130
|
2 |
|
]; |
131
|
2 |
|
return $conditions; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Gets the foreign key field name |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
6 |
View Code Duplication |
public function getForeignKey() |
|
|
|
|
140
|
|
|
{ |
141
|
6 |
|
if (is_null($this->foreignKey)) { |
142
|
6 |
|
$name = $this->getEntityDescriptor()->getTableName(); |
143
|
6 |
|
$name = Text::singular(strtolower($name)); |
144
|
6 |
|
$this->foreignKey = "{$name}_id"; |
145
|
6 |
|
} |
146
|
6 |
|
return $this->foreignKey; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check custom conditions |
151
|
|
|
* |
152
|
|
|
* @param Sql\Select $query |
153
|
|
|
* |
154
|
|
|
* @return self |
155
|
|
|
*/ |
156
|
2 |
|
protected function checkConditions(Sql\Select $query) |
157
|
|
|
{ |
158
|
2 |
|
if (null != $this->conditions) { |
159
|
2 |
|
$query->andWhere($this->conditions); |
160
|
2 |
|
} |
161
|
2 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check custom order |
166
|
|
|
* |
167
|
|
|
* @param Sql\Select $query |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
2 |
|
protected function checkOrder(Sql\Select $query) |
171
|
|
|
{ |
172
|
2 |
|
$order = $this->getEntityDescriptor()->getPrimaryKey()->getField(); |
173
|
2 |
|
$order .= " DESC"; |
174
|
2 |
|
if (null != $this->order) { |
175
|
2 |
|
$order = $this->order; |
176
|
2 |
|
} |
177
|
2 |
|
$query->order($order); |
178
|
2 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: