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\Contracts\Database\Eloquent\SupportsPartialRelations;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany;
10
use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels;
11
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
12
use Illuminate\Database\Query\JoinClause;
13
 
14
class MorphOne extends MorphOneOrMany implements SupportsPartialRelations
15
{
16
    use CanBeOneOfMany, ComparesRelatedModels, SupportsDefaultModels;
17
 
18
    /**
19
     * Get the results of the relationship.
20
     *
21
     * @return mixed
22
     */
23
    public function getResults()
24
    {
25
        if (is_null($this->getParentKey())) {
26
            return $this->getDefaultFor($this->parent);
27
        }
28
 
29
        return $this->query->first() ?: $this->getDefaultFor($this->parent);
30
    }
31
 
32
    /**
33
     * Initialize the relation on a set of models.
34
     *
35
     * @param  array  $models
36
     * @param  string  $relation
37
     * @return array
38
     */
39
    public function initRelation(array $models, $relation)
40
    {
41
        foreach ($models as $model) {
42
            $model->setRelation($relation, $this->getDefaultFor($model));
43
        }
44
 
45
        return $models;
46
    }
47
 
48
    /**
49
     * Match the eagerly loaded results to their parents.
50
     *
51
     * @param  array  $models
52
     * @param  \Illuminate\Database\Eloquent\Collection  $results
53
     * @param  string  $relation
54
     * @return array
55
     */
56
    public function match(array $models, Collection $results, $relation)
57
    {
58
        return $this->matchOne($models, $results, $relation);
59
    }
60
 
61
    /**
62
     * Get the relationship query.
63
     *
64
     * @param  \Illuminate\Database\Eloquent\Builder  $query
65
     * @param  \Illuminate\Database\Eloquent\Builder  $parentQuery
66
     * @param  array|mixed  $columns
67
     * @return \Illuminate\Database\Eloquent\Builder
68
     */
69
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
70
    {
71
        if ($this->isOneOfMany()) {
72
            $this->mergeOneOfManyJoinsTo($query);
73
        }
74
 
75
        return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
76
    }
77
 
78
    /**
79
     * Add constraints for inner join subselect for one of many relationships.
80
     *
81
     * @param  \Illuminate\Database\Eloquent\Builder  $query
82
     * @param  string|null  $column
83
     * @param  string|null  $aggregate
84
     * @return void
85
     */
86
    public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null)
87
    {
88
        $query->addSelect($this->foreignKey, $this->morphType);
89
    }
90
 
91
    /**
92
     * Get the columns that should be selected by the one of many subquery.
93
     *
94
     * @return array|string
95
     */
96
    public function getOneOfManySubQuerySelectColumns()
97
    {
98
        return [$this->foreignKey, $this->morphType];
99
    }
100
 
101
    /**
102
     * Add join query constraints for one of many relationships.
103
     *
104
     * @param  \Illuminate\Database\Query\JoinClause  $join
105
     * @return void
106
     */
107
    public function addOneOfManyJoinSubQueryConstraints(JoinClause $join)
108
    {
109
        $join
110
            ->on($this->qualifySubSelectColumn($this->morphType), '=', $this->qualifyRelatedColumn($this->morphType))
111
            ->on($this->qualifySubSelectColumn($this->foreignKey), '=', $this->qualifyRelatedColumn($this->foreignKey));
112
    }
113
 
114
    /**
115
     * Make a new related instance for the given model.
116
     *
117
     * @param  \Illuminate\Database\Eloquent\Model  $parent
118
     * @return \Illuminate\Database\Eloquent\Model
119
     */
120
    public function newRelatedInstanceFor(Model $parent)
121
    {
122
        return $this->related->newInstance()
123
                    ->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey})
124
                    ->setAttribute($this->getMorphType(), $this->morphClass);
125
    }
126
 
127
    /**
128
     * Get the value of the model's foreign key.
129
     *
130
     * @param  \Illuminate\Database\Eloquent\Model  $model
131
     * @return mixed
132
     */
133
    protected function getRelatedKeyFrom(Model $model)
134
    {
135
        return $model->getAttribute($this->getForeignKeyName());
136
    }
137
}