forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefinedConstraints.php
More file actions
127 lines (107 loc) · 3.8 KB
/
DefinedConstraints.php
File metadata and controls
127 lines (107 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php namespace October\Rain\Database\Relations;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as BelongsToManyBase;
use Illuminate\Support\Arr;
/**
* DefinedConstraints handles the constraints and filters defined by a relation
* eg: 'conditions' => 'is_published = 1'
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
trait DefinedConstraints
{
/**
* addDefinedConstraints to the relation query
*/
public function addDefinedConstraints(): void
{
$args = $this->getRelationDefinitionForDefinedConstraints();
$this->addDefinedConstraintsToRelation($this, $args);
$this->addDefinedConstraintsToQuery($this, $args);
}
/**
* addDefinedConstraintsToRelation
*/
public function addDefinedConstraintsToRelation($relation, ?array $args = null)
{
if ($args === null) {
$args = $this->getRelationDefinitionForDefinedConstraints();
}
// Default models (belongsTo, hasOne, hasOneThrough, morphOne)
if ($defaultData = Arr::get($args, 'default')) {
$relation->withDefault($defaultData);
}
// Pivot data (belongsToMany, morphToMany, morphByMany)
if ($pivotData = Arr::get($args, 'pivot')) {
$relation->withPivot($pivotData);
}
// Pivot incrementing key (belongsToMany, morphToMany, morphByMany)
if ($pivotKey = Arr::get($args, 'pivotKey')) {
$relation->withPivot($pivotKey);
}
// Pivot timestamps (belongsToMany, morphToMany, morphByMany)
if (Arr::get($args, 'timestamps')) {
$relation->withTimestamps();
}
// Count "helper" relation
// @deprecated use Laravel withCount() method instead
if (Arr::get($args, 'count')) {
if ($relation instanceof BelongsToManyBase) {
$relation->countMode = true;
$keyName = $relation->getQualifiedForeignPivotKeyName();
}
else {
$keyName = $relation->getForeignKeyName();
}
$countSql = $this->parent->getConnection()->raw('count(*) as count');
$relation->select($keyName, $countSql)->groupBy($keyName)->orderBy($keyName);
}
}
/**
* addDefinedConstraintsToQuery
*/
public function addDefinedConstraintsToQuery($query, ?array $args = null)
{
if ($args === null) {
$args = $this->getRelationDefinitionForDefinedConstraints();
}
// Conditions
if ($conditions = Arr::get($args, 'conditions')) {
$query->whereRaw($conditions);
}
// Sort order
// @deprecated count is deprecated
$hasCountArg = Arr::get($args, 'count') !== null;
if (($orderBy = Arr::get($args, 'order')) && !$hasCountArg) {
if (!is_array($orderBy)) {
$orderBy = [$orderBy];
}
foreach ($orderBy as $order) {
$column = $order;
$direction = 'asc';
$parts = explode(' ', $order);
if (count($parts) > 1) {
[$column, $direction] = $parts;
}
$query->orderBy($column, $direction);
}
}
// Scope
if ($scope = Arr::get($args, 'scope')) {
if (is_string($scope)) {
$query->$scope($this->parent);
}
else {
$scope($query, $this->parent, $this->related);
}
}
}
/**
* getRelationDefinitionForDefinedConstraints returns the relation definition for the
* relationship context.
*/
protected function getRelationDefinitionForDefinedConstraints()
{
return $this->parent->getRelationDefinition($this->relationName);
}
}