Repository navigation
Expand file tree
/
Copy pathMorphToMany.php
More file actions
223 lines (197 loc) · 5.45 KB
/
Copy pathMorphToMany.php
File metadata and controls
223 lines (197 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Relations;
use Hyperf\Collection\Arr;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use function Hyperf\Collection\collect;
/**
* @template TRelatedModel of \Hyperf\Database\Model\Model
* @template TDeclaringModel of \Hyperf\Database\Model\Model
* @template TPivotModel of \Hyperf\Database\Model\Relations\Pivot = \Hyperf\Database\Model\Relations\MorphPivot
* @template TAccessor of string = 'pivot'
*
* @extends BelongsToMany<TRelatedModel, TDeclaringModel, TPivotModel, TAccessor>
*/
class MorphToMany extends BelongsToMany
{
/**
* The type of the polymorphic relation.
*
* @var string
*/
protected $morphType;
/**
* The class name of the morph type constraint.
*
* @var string
*/
protected $morphClass;
/**
* Indicates if we are connecting the inverse of the relation.
*
* This primarily affects the morphClass constraint.
*
* @var bool
*/
protected $inverse;
/**
* Create a new morph to many relationship instance.
*
* @param string $name
* @param string $table
* @param string $foreignPivotKey
* @param string $relatedPivotKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relationName
* @param bool $inverse
*/
public function __construct(
Builder $query,
Model $parent,
$name,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName = null,
$inverse = false
) {
$this->inverse = $inverse;
$this->morphType = $name . '_type';
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();
parent::__construct(
$query,
$parent,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName
);
}
/**
* Set the constraints for an eager load of the relation.
*/
public function addEagerConstraints(array $models)
{
parent::addEagerConstraints($models);
$this->query->where($this->table . '.' . $this->morphType, $this->morphClass);
}
/**
* Add the constraints for a relationship count query.
*
* @param array|mixed $columns
* @return Builder
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where(
$this->table . '.' . $this->morphType,
$this->morphClass
);
}
/**
* Create a new pivot model instance.
*
* @param bool $exists
* @return Pivot
*/
public function newPivot(array $attributes = [], $exists = false)
{
$using = $this->using;
$pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists)
: MorphPivot::fromAttributes($this->parent, $attributes, $this->table, $exists);
$pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey)
->setMorphType($this->morphType)
->setMorphClass($this->morphClass);
return $pivot;
}
/**
* Get the foreign key "type" name.
*
* @return string
*/
public function getMorphType()
{
return $this->morphType;
}
/**
* Get the class name of the parent model.
*
* @return string
*/
public function getMorphClass()
{
return $this->morphClass;
}
/**
* Get the indicator for a reverse relationship.
*
* @return bool
*/
public function getInverse()
{
return $this->inverse;
}
/**
* Set the where clause for the relation query.
*
* @return $this
*/
protected function addWhereConstraints()
{
parent::addWhereConstraints();
$this->query->where($this->table . '.' . $this->morphType, $this->morphClass);
return $this;
}
/**
* Create a new pivot attachment record.
*
* @param int $id
* @param bool $timed
* @return array
*/
protected function baseAttachRecord($id, $timed)
{
return Arr::add(
parent::baseAttachRecord($id, $timed),
$this->morphType,
$this->morphClass
);
}
/**
* Create a new query builder for the pivot table.
*
* @return \Hyperf\Database\Query\Builder
*/
protected function newPivotQuery()
{
return parent::newPivotQuery()->where($this->morphType, $this->morphClass);
}
/**
* Get the pivot columns for the relation.
*
* "pivot_" is prefixed at each column for easy removal later.
*
* @return array
*/
protected function aliasedPivotColumns()
{
$defaults = [$this->foreignPivotKey, $this->relatedPivotKey, $this->morphType];
return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) {
return $this->table . '.' . $column . ' as pivot_' . $column;
})->unique()->all();
}
}