|
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
|
20 |
|
public function __construct($options) |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var \Slick\Orm\Annotations\BelongsTo $annotation */ |
|
59
|
20 |
|
$annotation = $options['annotation']; |
|
60
|
20 |
|
unset($options['annotation']); |
|
61
|
20 |
|
$options['foreignKey'] = $annotation->getParameter('foreignKey'); |
|
62
|
20 |
|
$options['parentEntity'] = $annotation->getValue(); |
|
63
|
20 |
|
$options['limit'] = $annotation->getParameter('limit'); |
|
64
|
20 |
|
$options['order'] = $annotation->getParameter('order'); |
|
65
|
20 |
|
$options['conditions'] = $annotation->getParameter('conditions'); |
|
66
|
|
|
|
|
67
|
20 |
|
parent::__construct($options); |
|
68
|
20 |
|
} |
|
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
|
|
|
/** @var EntityCollectionInterface $collection */ |
|
81
|
2 |
|
$collection = $repository->find() |
|
82
|
2 |
|
->where($this->getConditions($entity)) |
|
|
|
|
|
|
83
|
2 |
|
->limit($this->limit) |
|
84
|
2 |
|
->all(); |
|
85
|
|
|
$collection |
|
86
|
2 |
|
->setParentEntity($entity) |
|
87
|
2 |
|
->getEmitter() |
|
88
|
2 |
|
->addListener(EntityAdded::ACTION_ADD, [$this, 'add']); |
|
89
|
|
|
|
|
90
|
2 |
|
return $collection; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Saves the relation foreign key upon entity add |
|
95
|
|
|
* |
|
96
|
|
|
* @param EntityAdded $event |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function add(EntityAdded $event) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$entity = $event->getEntity(); |
|
101
|
2 |
|
$table = $this->getParentTableName(); |
|
102
|
2 |
|
$pmk = $this->getParentPrimaryKey(); |
|
103
|
2 |
|
$value = $event->getCollection()->parentEntity()->getId(); |
|
104
|
2 |
|
Sql::createSql($this->getAdapter()) |
|
105
|
2 |
|
->update($table) |
|
106
|
2 |
|
->set([$this->getForeignKey() => $value]) |
|
107
|
2 |
|
->where(["{$pmk} = :id" => [':id' => $entity->getId()]]) |
|
|
|
|
|
|
108
|
2 |
|
->execute(); |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Gets the relation conditions |
|
113
|
|
|
* |
|
114
|
|
|
* @param EntityInterface $entity |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
2 |
|
protected function getConditions(EntityInterface $entity) |
|
118
|
|
|
{ |
|
119
|
2 |
|
$field = "{$this->getParentTableName()}.{$this->getForeignKey()}"; |
|
120
|
2 |
|
$property = $this->getPropertyName(); |
|
121
|
|
|
$conditions = [ |
|
122
|
2 |
|
"{$field} = :{$property}" => [ |
|
123
|
2 |
|
":{$property}" => $entity->getId() |
|
124
|
1 |
|
] |
|
125
|
1 |
|
]; |
|
126
|
2 |
|
return $conditions; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets the foreign key field name |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
6 |
View Code Duplication |
public function getForeignKey() |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
6 |
|
if (is_null($this->foreignKey)) { |
|
137
|
6 |
|
$name = $this->getEntityDescriptor()->getTableName(); |
|
138
|
6 |
|
$name = Text::singular(strtolower($name)); |
|
139
|
6 |
|
$this->foreignKey = "{$name}_id"; |
|
140
|
3 |
|
} |
|
141
|
6 |
|
return $this->foreignKey; |
|
142
|
|
|
} |
|
143
|
|
|
} |
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: