|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Database\Query\AbstractQuery; |
|
6
|
|
|
use Nip\Database\Query\Delete as DeleteQuery; |
|
7
|
|
|
use Nip\Database\Query\Select as SelectQuery; |
|
8
|
|
|
use Nip\Records\Relations\Traits\HasCollectionResults; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class HasOneOrMany |
|
12
|
|
|
* @package Nip\Records\Relations |
|
13
|
|
|
*/ |
|
14
|
|
|
class MorphToMany extends HasAndBelongsToMany |
|
15
|
|
|
{ |
|
16
|
|
|
use HasCollectionResults; |
|
17
|
|
|
|
|
18
|
|
|
protected $type = 'morphToMany'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The type of the polymorphic relation. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $morphType = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Indicates if we are connecting the inverse of the relation. |
|
29
|
|
|
* |
|
30
|
|
|
* This primarily affects the morphClass constraint. |
|
31
|
|
|
* |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $inverse = false; |
|
35
|
|
|
|
|
36
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
37
|
|
|
* Builds the name of a has-and-belongs-to-many association table |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function generatePivotTable() |
|
41
|
|
|
{ |
|
42
|
|
|
$pivotManager = $this->getPivotManager(); |
|
43
|
|
|
|
|
44
|
|
|
return $pivotManager->getTable() . '_pivot'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return \Nip\Records\AbstractModels\RecordManager |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getPivotManager() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->isInverse() ? $this->getManager() : $this->getWith(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function isInverse(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->inverse; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param bool $inverse |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setInverse(bool $inverse) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->inverse = $inverse; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param AbstractQuery $query |
|
73
|
|
|
* @return AbstractQuery |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function populateQuerySpecific(AbstractQuery $query) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->isInverse()) { |
|
78
|
|
|
return parent::populateQuerySpecific($query); |
|
79
|
|
|
} |
|
80
|
|
|
$pk1 = $this->getManager()->getPrimaryKey(); |
|
81
|
|
|
|
|
82
|
|
|
$query->where("`{$this->getTable()}`.`pivotal_id` = ?", $this->getItem()->{$pk1}); |
|
83
|
|
|
|
|
84
|
|
|
return $query; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function formatAttachData($record) |
|
91
|
|
|
{ |
|
92
|
|
|
$data = parent::formatAttachData($record); |
|
93
|
|
|
$data[$this->getMorphKey()] = $this->getMorphType(); |
|
94
|
|
|
return $data; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the foreign key "type" name. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getMorphKey() |
|
103
|
|
|
{ |
|
104
|
|
|
return 'pivotal_type'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the foreign key "type" name. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getMorphType() |
|
113
|
|
|
{ |
|
114
|
|
|
if ($this->morphType == null) { |
|
115
|
|
|
$this->initMorphType(); |
|
116
|
|
|
} |
|
117
|
|
|
return $this->morphType; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $morphType |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setMorphType(string $morphType) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->morphType = $morphType; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function initMorphType() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->setMorphType($this->generateMorphType()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function generateMorphType() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->isInverse() ? $this->getWith()->getTable() : $this->getManager()->getTable(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param DeleteQuery $query |
|
143
|
|
|
* @param $records |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function queryDetachRecords($query, $records) |
|
146
|
|
|
{ |
|
147
|
|
|
parent::queryDetachRecords($query, $records); |
|
148
|
|
|
$query->where("`{$this->getMorphKey()}` = ?", $this->getMorphType()); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param SelectQuery $query |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function hydrateQueryWithPivotConstraints($query) |
|
155
|
|
|
{ |
|
156
|
|
|
parent::hydrateQueryWithPivotConstraints($query); |
|
157
|
|
|
$query->where( |
|
158
|
|
|
"`{$this->getTable()}`.`{$this->getMorphKey()}` = ?", |
|
159
|
|
|
$this->getMorphType() |
|
|
|
|
|
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return mixed |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getPivotFK() |
|
167
|
|
|
{ |
|
168
|
|
|
if ($this->isInverse()) { |
|
169
|
|
|
return 'pivotal_id'; |
|
170
|
|
|
} |
|
171
|
|
|
return parent::getPivotFK(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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.