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
|
|
|
use Slick\Common\Utils\Text; |
12
|
|
|
use Slick\Database\Sql\Select; |
13
|
|
|
use Slick\Orm\Entity\EntityCollectionInterface; |
14
|
|
|
use Slick\Orm\EntityInterface; |
15
|
|
|
use Slick\Orm\Event\EntityAdded; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* HasAndBelongsToMany relation |
19
|
|
|
* |
20
|
|
|
* @package Slick\Orm\Mapper\Relation |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class HasAndBelongsToMany extends HasMany |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @readwrite |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $relatedForeignKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @readwrite |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $relationTable; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets the related foreign key |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
4 |
|
public function getRelatedForeignKey() |
44
|
|
|
{ |
45
|
4 |
|
if (null == $this->relatedForeignKey) { |
46
|
4 |
|
$name = $this->getParentTableName(); |
47
|
4 |
|
$this->relatedForeignKey = "{$this->normalizeFieldName($name)}_id"; |
48
|
4 |
|
} |
49
|
4 |
|
return $this->relatedForeignKey; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the related table |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
4 |
|
public function getRelationTable() |
58
|
|
|
{ |
59
|
4 |
|
if (is_null($this->relationTable)) { |
60
|
4 |
|
$parentTable = $this->getParentTableName(); |
61
|
4 |
|
$table = $this->getEntityDescriptor()->getTableName(); |
62
|
4 |
|
$names = [$parentTable, $table]; |
63
|
4 |
|
asort($names); |
64
|
4 |
|
$first = array_shift($names); |
65
|
4 |
|
$tableName = $this->normalizeFieldName($first); |
66
|
4 |
|
array_unshift($names, $tableName); |
67
|
4 |
|
$this->relationTable = implode('_', $names); |
68
|
4 |
|
} |
69
|
4 |
|
return $this->relationTable; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Loads the entity or entity collection for this relation |
74
|
|
|
* |
75
|
|
|
* @param EntityInterface $entity |
76
|
|
|
* |
77
|
|
|
* @return EntityCollectionInterface|EntityInterface[] |
78
|
|
|
*/ |
79
|
2 |
View Code Duplication |
public function load(EntityInterface $entity) |
|
|
|
|
80
|
|
|
{ |
81
|
2 |
|
$repository = $this->getParentRepository(); |
82
|
|
|
|
83
|
2 |
|
$query = $repository->find() |
84
|
2 |
|
->where($this->getConditions($entity)) |
|
|
|
|
85
|
2 |
|
->limit($this->limit); |
86
|
2 |
|
$this->addJoinTable($query); |
87
|
2 |
|
$this->checkConditions($query) |
88
|
2 |
|
->checkOrder($query); |
89
|
|
|
|
90
|
|
|
/** @var EntityCollectionInterface $collection */ |
91
|
2 |
|
$collection = $query->all(); |
92
|
|
|
|
93
|
|
|
$collection |
94
|
2 |
|
->setParentEntity($entity) |
95
|
2 |
|
->getEmitter() |
96
|
2 |
|
->addListener(EntityAdded::ACTION_ADD, [$this, 'add']); |
97
|
|
|
|
98
|
2 |
|
return $collection; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Saves the relation row upon entity add |
103
|
|
|
* |
104
|
|
|
* @param EntityAdded $event |
105
|
|
|
*/ |
106
|
|
|
public function add(EntityAdded $event) |
107
|
|
|
{ |
108
|
|
|
// TODO: Change the autogenerated stub |
109
|
|
|
} |
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 |
|
$parts = explode('\\', $this->getEntityDescriptor()->className()); |
120
|
2 |
|
$property = lcfirst(array_pop($parts)); |
121
|
|
|
return [ |
122
|
2 |
|
"rel.{$this->getForeignKey()} = :{$property}" => [ |
123
|
2 |
|
":{$property}" => $entity->getId() |
124
|
2 |
|
] |
125
|
2 |
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds the join table to the query |
130
|
|
|
* |
131
|
|
|
* @param Select $query |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
2 |
|
protected function addJoinTable(Select $query) |
135
|
|
|
{ |
136
|
2 |
|
$relationTable = $this->getRelationTable(); |
137
|
2 |
|
$table = $this->getParentTableName(); |
138
|
2 |
|
$pmk = $this->getParentPrimaryKey(); |
139
|
2 |
|
$rfk = $this->getRelatedForeignKey(); |
140
|
2 |
|
$query->join( |
141
|
2 |
|
$relationTable, |
142
|
2 |
|
"rel.{$rfk} = {$table}.{$pmk}", |
143
|
2 |
|
null, |
144
|
|
|
'rel' |
145
|
2 |
|
); |
146
|
2 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.