Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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