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
 
8
abstract class MorphOneOrMany extends HasOneOrMany
9
{
10
    /**
11
     * The foreign key type for the relationship.
12
     *
13
     * @var string
14
     */
15
    protected $morphType;
16
 
17
    /**
18
     * The class name of the parent model.
19
     *
20
     * @var string
21
     */
22
    protected $morphClass;
23
 
24
    /**
25
     * Create a new morph one or many relationship instance.
26
     *
27
     * @param  \Illuminate\Database\Eloquent\Builder  $query
28
     * @param  \Illuminate\Database\Eloquent\Model  $parent
29
     * @param  string  $type
30
     * @param  string  $id
31
     * @param  string  $localKey
32
     * @return void
33
     */
34
    public function __construct(Builder $query, Model $parent, $type, $id, $localKey)
35
    {
36
        $this->morphType = $type;
37
 
38
        $this->morphClass = $parent->getMorphClass();
39
 
40
        parent::__construct($query, $parent, $id, $localKey);
41
    }
42
 
43
    /**
44
     * Set the base constraints on the relation query.
45
     *
46
     * @return void
47
     */
48
    public function addConstraints()
49
    {
50
        if (static::$constraints) {
51
            $this->getRelationQuery()->where($this->morphType, $this->morphClass);
52
 
53
            parent::addConstraints();
54
        }
55
    }
56
 
57
    /**
58
     * Set the constraints for an eager load of the relation.
59
     *
60
     * @param  array  $models
61
     * @return void
62
     */
63
    public function addEagerConstraints(array $models)
64
    {
65
        parent::addEagerConstraints($models);
66
 
67
        $this->getRelationQuery()->where($this->morphType, $this->morphClass);
68
    }
69
 
70
    /**
71
     * Set the foreign ID and type for creating a related model.
72
     *
73
     * @param  \Illuminate\Database\Eloquent\Model  $model
74
     * @return void
75
     */
76
    protected function setForeignAttributesForCreate(Model $model)
77
    {
78
        $model->{$this->getForeignKeyName()} = $this->getParentKey();
79
 
80
        $model->{$this->getMorphType()} = $this->morphClass;
81
    }
82
 
83
    /**
84
     * Get the relationship query.
85
     *
86
     * @param  \Illuminate\Database\Eloquent\Builder  $query
87
     * @param  \Illuminate\Database\Eloquent\Builder  $parentQuery
88
     * @param  array|mixed  $columns
89
     * @return \Illuminate\Database\Eloquent\Builder
90
     */
91
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
92
    {
93
        return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where(
94
            $query->qualifyColumn($this->getMorphType()), $this->morphClass
95
        );
96
    }
97
 
98
    /**
99
     * Get the foreign key "type" name.
100
     *
101
     * @return string
102
     */
103
    public function getQualifiedMorphType()
104
    {
105
        return $this->morphType;
106
    }
107
 
108
    /**
109
     * Get the plain morph type name without the table.
110
     *
111
     * @return string
112
     */
113
    public function getMorphType()
114
    {
115
        return last(explode('.', $this->morphType));
116
    }
117
 
118
    /**
119
     * Get the class name of the parent model.
120
     *
121
     * @return string
122
     */
123
    public function getMorphClass()
124
    {
125
        return $this->morphClass;
126
    }
127
}