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\Concerns;
4
 
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
 
8
trait AsPivot
9
{
10
    /**
11
     * The parent model of the relationship.
12
     *
13
     * @var \Illuminate\Database\Eloquent\Model
14
     */
15
    public $pivotParent;
16
 
17
    /**
18
     * The name of the foreign key column.
19
     *
20
     * @var string
21
     */
22
    protected $foreignKey;
23
 
24
    /**
25
     * The name of the "other key" column.
26
     *
27
     * @var string
28
     */
29
    protected $relatedKey;
30
 
31
    /**
32
     * Create a new pivot model instance.
33
     *
34
     * @param  \Illuminate\Database\Eloquent\Model  $parent
35
     * @param  array  $attributes
36
     * @param  string  $table
37
     * @param  bool  $exists
38
     * @return static
39
     */
40
    public static function fromAttributes(Model $parent, $attributes, $table, $exists = false)
41
    {
42
        $instance = new static;
43
 
44
        $instance->timestamps = $instance->hasTimestampAttributes($attributes);
45
 
46
        // The pivot model is a "dynamic" model since we will set the tables dynamically
47
        // for the instance. This allows it work for any intermediate tables for the
48
        // many to many relationship that are defined by this developer's classes.
49
        $instance->setConnection($parent->getConnectionName())
50
            ->setTable($table)
51
            ->forceFill($attributes)
52
            ->syncOriginal();
53
 
54
        // We store off the parent instance so we will access the timestamp column names
55
        // for the model, since the pivot model timestamps aren't easily configurable
56
        // from the developer's point of view. We can use the parents to get these.
57
        $instance->pivotParent = $parent;
58
 
59
        $instance->exists = $exists;
60
 
61
        return $instance;
62
    }
63
 
64
    /**
65
     * Create a new pivot model from raw values returned from a query.
66
     *
67
     * @param  \Illuminate\Database\Eloquent\Model  $parent
68
     * @param  array  $attributes
69
     * @param  string  $table
70
     * @param  bool  $exists
71
     * @return static
72
     */
73
    public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = false)
74
    {
75
        $instance = static::fromAttributes($parent, [], $table, $exists);
76
 
77
        $instance->timestamps = $instance->hasTimestampAttributes($attributes);
78
 
79
        $instance->setRawAttributes(
80
            array_merge($instance->getRawOriginal(), $attributes), $exists
81
        );
82
 
83
        return $instance;
84
    }
85
 
86
    /**
87
     * Set the keys for a select query.
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Builder  $query
90
     * @return \Illuminate\Database\Eloquent\Builder
91
     */
92
    protected function setKeysForSelectQuery($query)
93
    {
94
        if (isset($this->attributes[$this->getKeyName()])) {
95
            return parent::setKeysForSelectQuery($query);
96
        }
97
 
98
        $query->where($this->foreignKey, $this->getOriginal(
99
            $this->foreignKey, $this->getAttribute($this->foreignKey)
100
        ));
101
 
102
        return $query->where($this->relatedKey, $this->getOriginal(
103
            $this->relatedKey, $this->getAttribute($this->relatedKey)
104
        ));
105
    }
106
 
107
    /**
108
     * Set the keys for a save update query.
109
     *
110
     * @param  \Illuminate\Database\Eloquent\Builder  $query
111
     * @return \Illuminate\Database\Eloquent\Builder
112
     */
113
    protected function setKeysForSaveQuery($query)
114
    {
115
        return $this->setKeysForSelectQuery($query);
116
    }
117
 
118
    /**
119
     * Delete the pivot model record from the database.
120
     *
121
     * @return int
122
     */
123
    public function delete()
124
    {
125
        if (isset($this->attributes[$this->getKeyName()])) {
126
            return (int) parent::delete();
127
        }
128
 
129
        if ($this->fireModelEvent('deleting') === false) {
130
            return 0;
131
        }
132
 
133
        $this->touchOwners();
134
 
135
        return tap($this->getDeleteQuery()->delete(), function () {
136
            $this->exists = false;
137
 
138
            $this->fireModelEvent('deleted', false);
139
        });
140
    }
141
 
142
    /**
143
     * Get the query builder for a delete operation on the pivot.
144
     *
145
     * @return \Illuminate\Database\Eloquent\Builder
146
     */
147
    protected function getDeleteQuery()
148
    {
149
        return $this->newQueryWithoutRelationships()->where([
150
            $this->foreignKey => $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey)),
151
            $this->relatedKey => $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey)),
152
        ]);
153
    }
154
 
155
    /**
156
     * Get the table associated with the model.
157
     *
158
     * @return string
159
     */
160
    public function getTable()
161
    {
162
        if (! isset($this->table)) {
163
            $this->setTable(str_replace(
164
                '\\', '', Str::snake(Str::singular(class_basename($this)))
165
            ));
166
        }
167
 
168
        return $this->table;
169
    }
170
 
171
    /**
172
     * Get the foreign key column name.
173
     *
174
     * @return string
175
     */
176
    public function getForeignKey()
177
    {
178
        return $this->foreignKey;
179
    }
180
 
181
    /**
182
     * Get the "related key" column name.
183
     *
184
     * @return string
185
     */
186
    public function getRelatedKey()
187
    {
188
        return $this->relatedKey;
189
    }
190
 
191
    /**
192
     * Get the "related key" column name.
193
     *
194
     * @return string
195
     */
196
    public function getOtherKey()
197
    {
198
        return $this->getRelatedKey();
199
    }
200
 
201
    /**
202
     * Set the key names for the pivot model instance.
203
     *
204
     * @param  string  $foreignKey
205
     * @param  string  $relatedKey
206
     * @return $this
207
     */
208
    public function setPivotKeys($foreignKey, $relatedKey)
209
    {
210
        $this->foreignKey = $foreignKey;
211
 
212
        $this->relatedKey = $relatedKey;
213
 
214
        return $this;
215
    }
216
 
217
    /**
218
     * Determine if the pivot model or given attributes has timestamp attributes.
219
     *
220
     * @param  array|null  $attributes
221
     * @return bool
222
     */
223
    public function hasTimestampAttributes($attributes = null)
224
    {
225
        return array_key_exists($this->getCreatedAtColumn(), $attributes ?? $this->attributes);
226
    }
227
 
228
    /**
229
     * Get the name of the "created at" column.
230
     *
231
     * @return string
232
     */
233
    public function getCreatedAtColumn()
234
    {
235
        return $this->pivotParent
236
            ? $this->pivotParent->getCreatedAtColumn()
237
            : parent::getCreatedAtColumn();
238
    }
239
 
240
    /**
241
     * Get the name of the "updated at" column.
242
     *
243
     * @return string
244
     */
245
    public function getUpdatedAtColumn()
246
    {
247
        return $this->pivotParent
248
            ? $this->pivotParent->getUpdatedAtColumn()
249
            : parent::getUpdatedAtColumn();
250
    }
251
 
252
    /**
253
     * Get the queueable identity for the entity.
254
     *
255
     * @return mixed
256
     */
257
    public function getQueueableId()
258
    {
259
        if (isset($this->attributes[$this->getKeyName()])) {
260
            return $this->getKey();
261
        }
262
 
263
        return sprintf(
264
            '%s:%s:%s:%s',
265
            $this->foreignKey, $this->getAttribute($this->foreignKey),
266
            $this->relatedKey, $this->getAttribute($this->relatedKey)
267
        );
268
    }
269
 
270
    /**
271
     * Get a new query to restore one or more models by their queueable IDs.
272
     *
273
     * @param  int[]|string[]|string  $ids
274
     * @return \Illuminate\Database\Eloquent\Builder
275
     */
276
    public function newQueryForRestoration($ids)
277
    {
278
        if (is_array($ids)) {
279
            return $this->newQueryForCollectionRestoration($ids);
280
        }
281
 
282
        if (! str_contains($ids, ':')) {
283
            return parent::newQueryForRestoration($ids);
284
        }
285
 
286
        $segments = explode(':', $ids);
287
 
288
        return $this->newQueryWithoutScopes()
289
            ->where($segments[0], $segments[1])
290
            ->where($segments[2], $segments[3]);
291
    }
292
 
293
    /**
294
     * Get a new query to restore multiple models by their queueable IDs.
295
     *
296
     * @param  int[]|string[]  $ids
297
     * @return \Illuminate\Database\Eloquent\Builder
298
     */
299
    protected function newQueryForCollectionRestoration(array $ids)
300
    {
301
        $ids = array_values($ids);
302
 
303
        if (! str_contains($ids[0], ':')) {
304
            return parent::newQueryForRestoration($ids);
305
        }
306
 
307
        $query = $this->newQueryWithoutScopes();
308
 
309
        foreach ($ids as $id) {
310
            $segments = explode(':', $id);
311
 
312
            $query->orWhere(function ($query) use ($segments) {
313
                return $query->where($segments[0], $segments[1])
314
                    ->where($segments[2], $segments[3]);
315
            });
316
        }
317
 
318
        return $query;
319
    }
320
 
321
    /**
322
     * Unset all the loaded relations for the instance.
323
     *
324
     * @return $this
325
     */
326
    public function unsetRelations()
327
    {
328
        $this->pivotParent = null;
329
        $this->relations = [];
330
 
331
        return $this;
332
    }
333
}