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
class MorphPivot extends Pivot
6
{
7
    /**
8
     * The type of the polymorphic relation.
9
     *
10
     * Explicitly define this so it's not included in saved attributes.
11
     *
12
     * @var string
13
     */
14
    protected $morphType;
15
 
16
    /**
17
     * The value of the polymorphic relation.
18
     *
19
     * Explicitly define this so it's not included in saved attributes.
20
     *
21
     * @var string
22
     */
23
    protected $morphClass;
24
 
25
    /**
26
     * Set the keys for a save update query.
27
     *
28
     * @param  \Illuminate\Database\Eloquent\Builder  $query
29
     * @return \Illuminate\Database\Eloquent\Builder
30
     */
31
    protected function setKeysForSaveQuery($query)
32
    {
33
        $query->where($this->morphType, $this->morphClass);
34
 
35
        return parent::setKeysForSaveQuery($query);
36
    }
37
 
38
    /**
39
     * Set the keys for a select query.
40
     *
41
     * @param  \Illuminate\Database\Eloquent\Builder  $query
42
     * @return \Illuminate\Database\Eloquent\Builder
43
     */
44
    protected function setKeysForSelectQuery($query)
45
    {
46
        $query->where($this->morphType, $this->morphClass);
47
 
48
        return parent::setKeysForSelectQuery($query);
49
    }
50
 
51
    /**
52
     * Delete the pivot model record from the database.
53
     *
54
     * @return int
55
     */
56
    public function delete()
57
    {
58
        if (isset($this->attributes[$this->getKeyName()])) {
59
            return (int) parent::delete();
60
        }
61
 
62
        if ($this->fireModelEvent('deleting') === false) {
63
            return 0;
64
        }
65
 
66
        $query = $this->getDeleteQuery();
67
 
68
        $query->where($this->morphType, $this->morphClass);
69
 
70
        return tap($query->delete(), function () {
71
            $this->fireModelEvent('deleted', false);
72
        });
73
    }
74
 
75
    /**
76
     * Get the morph type for the pivot.
77
     *
78
     * @return string
79
     */
80
    public function getMorphType()
81
    {
82
        return $this->morphType;
83
    }
84
 
85
    /**
86
     * Set the morph type for the pivot.
87
     *
88
     * @param  string  $morphType
89
     * @return $this
90
     */
91
    public function setMorphType($morphType)
92
    {
93
        $this->morphType = $morphType;
94
 
95
        return $this;
96
    }
97
 
98
    /**
99
     * Set the morph class for the pivot.
100
     *
101
     * @param  string  $morphClass
102
     * @return \Illuminate\Database\Eloquent\Relations\MorphPivot
103
     */
104
    public function setMorphClass($morphClass)
105
    {
106
        $this->morphClass = $morphClass;
107
 
108
        return $this;
109
    }
110
 
111
    /**
112
     * Get the queueable identity for the entity.
113
     *
114
     * @return mixed
115
     */
116
    public function getQueueableId()
117
    {
118
        if (isset($this->attributes[$this->getKeyName()])) {
119
            return $this->getKey();
120
        }
121
 
122
        return sprintf(
123
            '%s:%s:%s:%s:%s:%s',
124
            $this->foreignKey, $this->getAttribute($this->foreignKey),
125
            $this->relatedKey, $this->getAttribute($this->relatedKey),
126
            $this->morphType, $this->morphClass
127
        );
128
    }
129
 
130
    /**
131
     * Get a new query to restore one or more models by their queueable IDs.
132
     *
133
     * @param  array|int  $ids
134
     * @return \Illuminate\Database\Eloquent\Builder
135
     */
136
    public function newQueryForRestoration($ids)
137
    {
138
        if (is_array($ids)) {
139
            return $this->newQueryForCollectionRestoration($ids);
140
        }
141
 
142
        if (! str_contains($ids, ':')) {
143
            return parent::newQueryForRestoration($ids);
144
        }
145
 
146
        $segments = explode(':', $ids);
147
 
148
        return $this->newQueryWithoutScopes()
149
                        ->where($segments[0], $segments[1])
150
                        ->where($segments[2], $segments[3])
151
                        ->where($segments[4], $segments[5]);
152
    }
153
 
154
    /**
155
     * Get a new query to restore multiple models by their queueable IDs.
156
     *
157
     * @param  array  $ids
158
     * @return \Illuminate\Database\Eloquent\Builder
159
     */
160
    protected function newQueryForCollectionRestoration(array $ids)
161
    {
162
        $ids = array_values($ids);
163
 
164
        if (! str_contains($ids[0], ':')) {
165
            return parent::newQueryForRestoration($ids);
166
        }
167
 
168
        $query = $this->newQueryWithoutScopes();
169
 
170
        foreach ($ids as $id) {
171
            $segments = explode(':', $id);
172
 
173
            $query->orWhere(function ($query) use ($segments) {
174
                return $query->where($segments[0], $segments[1])
175
                             ->where($segments[2], $segments[3])
176
                             ->where($segments[4], $segments[5]);
177
            });
178
        }
179
 
180
        return $query;
181
    }
182
}