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 HasOne extends HasOneOrMany implements SupportsPartialRelations
15
{
16
    use ComparesRelatedModels, CanBeOneOfMany, 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
     * Add the constraints for an internal relationship existence query.
63
     *
64
     * Essentially, these queries compare on column names like "whereColumn".
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Builder  $query
67
     * @param  \Illuminate\Database\Eloquent\Builder  $parentQuery
68
     * @param  array|mixed  $columns
69
     * @return \Illuminate\Database\Eloquent\Builder
70
     */
71
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
72
    {
73
        if ($this->isOneOfMany()) {
74
            $this->mergeOneOfManyJoinsTo($query);
75
        }
76
 
77
        return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
78
    }
79
 
80
    /**
81
     * Add constraints for inner join subselect for one of many relationships.
82
     *
83
     * @param  \Illuminate\Database\Eloquent\Builder  $query
84
     * @param  string|null  $column
85
     * @param  string|null  $aggregate
86
     * @return void
87
     */
88
    public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null)
89
    {
90
        $query->addSelect($this->foreignKey);
91
    }
92
 
93
    /**
94
     * Get the columns that should be selected by the one of many subquery.
95
     *
96
     * @return array|string
97
     */
98
    public function getOneOfManySubQuerySelectColumns()
99
    {
100
        return $this->foreignKey;
101
    }
102
 
103
    /**
104
     * Add join query constraints for one of many relationships.
105
     *
106
     * @param  \Illuminate\Database\Query\JoinClause  $join
107
     * @return void
108
     */
109
    public function addOneOfManyJoinSubQueryConstraints(JoinClause $join)
110
    {
111
        $join->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()->setAttribute(
123
            $this->getForeignKeyName(), $parent->{$this->localKey}
124
        );
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
}