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\Orm\Entity\EntityCollection; |
14
|
|
|
use Slick\Orm\EntityInterface; |
15
|
|
|
use Slick\Orm\Mapper\RelationInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* HasMany |
19
|
|
|
* |
20
|
|
|
* @package Slick\Orm\Mapper\Relation |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class HasMany extends AbstractRelation implements RelationInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Relations utility methods |
27
|
|
|
*/ |
28
|
|
|
use RelationsUtilityMethods; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @readwrite |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
protected $limit; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @readwrite |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $order; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @readwrite |
44
|
|
|
* @var mixed |
45
|
|
|
*/ |
46
|
|
|
protected $conditions; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* BelongsTo relation |
50
|
|
|
* |
51
|
|
|
* @param array|object $options The parameters from annotation |
52
|
|
|
*/ |
53
|
18 |
|
public function __construct($options) |
54
|
|
|
{ |
55
|
|
|
/** @var \Slick\Orm\Annotations\BelongsTo $annotation */ |
56
|
18 |
|
$annotation = $options['annotation']; |
57
|
18 |
|
unset($options['annotation']); |
58
|
18 |
|
$options['foreignKey'] = $annotation->getParameter('foreignKey'); |
59
|
18 |
|
$options['parentEntity'] = $annotation->getValue(); |
60
|
18 |
|
$options['limit'] = $annotation->getParameter('limit'); |
61
|
18 |
|
$options['order'] = $annotation->getParameter('order'); |
62
|
18 |
|
$options['conditions'] = $annotation->getParameter('conditions'); |
63
|
|
|
|
64
|
18 |
|
parent::__construct($options); |
65
|
18 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Loads the entity or entity collection for this relation |
69
|
|
|
* |
70
|
|
|
* @param EntityInterface $entity |
71
|
|
|
* |
72
|
|
|
* @return null|EntityInterface|EntityCollection|EntityInterface[] |
73
|
|
|
*/ |
74
|
2 |
|
public function load(EntityInterface $entity) |
75
|
|
|
{ |
76
|
2 |
|
$repository = $this->getParentRepository(); |
77
|
2 |
|
$collection = $repository->find() |
78
|
2 |
|
->where($this->getConditions($entity)) |
|
|
|
|
79
|
2 |
|
->limit($this->limit) |
80
|
2 |
|
->all(); |
81
|
2 |
|
return $collection; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the relation conditions |
86
|
|
|
* |
87
|
|
|
* @param EntityInterface $entity |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
2 |
|
protected function getConditions(EntityInterface $entity) |
91
|
|
|
{ |
92
|
2 |
|
$field = "{$this->getParentTableName()}.{$this->getForeignKey()}"; |
93
|
2 |
|
$property = $this->getPropertyName(); |
94
|
|
|
$conditions = [ |
95
|
2 |
|
"{$field} = :{$property}" => [ |
96
|
2 |
|
":{$property}" => $entity->getId() |
97
|
2 |
|
] |
98
|
2 |
|
]; |
99
|
2 |
|
return $conditions; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the foreign key field name |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
4 |
View Code Duplication |
public function getForeignKey() |
|
|
|
|
108
|
|
|
{ |
109
|
4 |
|
if (is_null($this->foreignKey)) { |
110
|
4 |
|
$name = $this->getEntityDescriptor()->getTableName(); |
111
|
4 |
|
$name = Text::singular(strtolower($name)); |
112
|
4 |
|
$this->foreignKey = "{$name}_id"; |
113
|
4 |
|
} |
114
|
4 |
|
return $this->foreignKey; |
115
|
|
|
} |
116
|
|
|
} |
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: