|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Analogue\ORM\Relationships; |
|
4
|
|
|
|
|
5
|
|
|
use Analogue\ORM\System\Mapper; |
|
6
|
|
|
use Analogue\ORM\System\Query; |
|
7
|
|
|
|
|
8
|
|
|
class MorphToMany extends BelongsToMany |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The type of the polymorphic relation. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $morphType; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The class name of the morph type constraint. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $morphClass; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Indicates if we are connecting the inverse of the relation. |
|
26
|
|
|
* |
|
27
|
|
|
* This primarily affects the morphClass constraint. |
|
28
|
|
|
* |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $inverse; |
|
32
|
|
|
|
|
33
|
|
|
protected static $hasPivot = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new has many relationship instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Mapper $mapper |
|
39
|
|
|
* @param \Analogue\ORM\Entity $parent |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param string $table |
|
42
|
|
|
* @param string $foreignKey |
|
43
|
|
|
* @param string $otherKey |
|
44
|
|
|
* @param string|null $relationName |
|
45
|
|
|
* @param bool $inverse |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->inverse = $inverse; |
|
50
|
|
|
|
|
51
|
|
|
$this->morphType = $name . '_type'; |
|
52
|
|
|
|
|
53
|
|
|
$this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent); |
|
54
|
|
|
|
|
55
|
|
|
parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the where clause for the relation query. |
|
60
|
|
|
* |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function setWhere() |
|
64
|
|
|
{ |
|
65
|
|
|
parent::setWhere(); |
|
66
|
|
|
|
|
67
|
|
|
$this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add the constraints for a relationship count query. |
|
74
|
|
|
* |
|
75
|
|
|
* @param Query $query |
|
76
|
|
|
* @param Query $parent |
|
77
|
|
|
* @return Query |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getRelationCountQuery(Query $query, Query $parent) |
|
80
|
|
|
{ |
|
81
|
|
|
$query = parent::getRelationCountQuery($query, $parent); |
|
82
|
|
|
|
|
83
|
|
|
return $query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set the constraints for an eager load of the relation. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $entities |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addEagerConstraints(array $entities) |
|
93
|
|
|
{ |
|
94
|
|
|
parent::addEagerConstraints($entities); |
|
95
|
|
|
|
|
96
|
|
|
$this->query->where($this->table . '.' . $this->morphType, $this->morphClass); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create a new pivot attachment record. |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $id |
|
103
|
|
|
* @param bool $timed |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function createAttachRecord($id, $timed) |
|
107
|
|
|
{ |
|
108
|
|
|
$record = parent::createAttachRecord($id, $timed); |
|
109
|
|
|
|
|
110
|
|
|
return array_add($record, $this->morphType, $this->morphClass); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Create a new query builder for the pivot table. |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \InvalidArgumentException |
|
117
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function newPivotQuery() |
|
120
|
|
|
{ |
|
121
|
|
|
$query = parent::newPivotQuery(); |
|
122
|
|
|
|
|
123
|
|
|
return $query->where($this->morphType, $this->morphClass); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Create a new pivot model instance. |
|
128
|
|
|
* |
|
129
|
|
|
* @param array $attributes |
|
130
|
|
|
* @param bool $exists |
|
131
|
|
|
* @return Pivot |
|
132
|
|
|
*/ |
|
133
|
|
|
public function newPivot(array $attributes = [], $exists = false) |
|
134
|
|
|
{ |
|
135
|
|
|
$pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists); |
|
136
|
|
|
|
|
137
|
|
|
$pivot->setPivotKeys($this->foreignKey, $this->otherKey) |
|
138
|
|
|
->setMorphType($this->morphType) |
|
139
|
|
|
->setMorphClass($this->morphClass); |
|
140
|
|
|
|
|
141
|
|
|
return $pivot; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Return Pivot attributes when available on a relationship |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getPivotAttributes() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->pivotColumns; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the foreign key "type" name. |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getMorphType() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->morphType; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get the class name of the parent model. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getMorphClass() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->morphClass; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|