1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blasttech\EloquentRelatedPlus; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait HelperMethodTrait |
10
|
|
|
* |
11
|
|
|
* @property array order_fields |
12
|
|
|
* @property array order_defaults |
13
|
|
|
* @property array order_relations |
14
|
|
|
* @property array order_with |
15
|
|
|
* @property array search_fields |
16
|
|
|
* @property string connection |
17
|
|
|
*/ |
18
|
|
|
trait HelperMethodTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Get relation table name and alias |
22
|
|
|
* |
23
|
|
|
* @param Relation $relation |
24
|
|
|
* @return \stdClass |
25
|
|
|
*/ |
26
|
|
|
protected function getRelationTables($relation) |
27
|
|
|
{ |
28
|
|
|
$table = new \stdClass(); |
29
|
|
|
$table->name = $relation->getRelated()->getTable(); |
30
|
|
|
// if using a 'table' AS 'tableAlias' in a from statement, otherwise alias will be the table name |
31
|
|
|
$from = explode(' ', $relation->getQuery()->getQuery()->from); |
32
|
|
|
$table->alias = array_pop($from); |
33
|
|
|
|
34
|
|
|
return $table; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the relations from a relation name |
39
|
|
|
* $relationName can be a single relation |
40
|
|
|
* Usage for User model: |
41
|
|
|
* parseRelationNames('customer') returns [$user->customer()] |
42
|
|
|
* parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()] |
43
|
|
|
* |
44
|
|
|
* @param string $relationName |
45
|
|
|
* @return Relation[] |
46
|
|
|
*/ |
47
|
|
|
protected function parseRelationNames($relationName) |
48
|
|
|
{ |
49
|
|
|
$relationNames = explode('.', $relationName); |
50
|
|
|
$parentRelationName = null; |
51
|
|
|
$relations = []; |
52
|
|
|
|
53
|
|
|
foreach ($relationNames as $relationName) { |
|
|
|
|
54
|
|
|
if (is_null($parentRelationName)) { |
55
|
|
|
$relations[] = $this->$relationName(); |
56
|
|
|
$parentRelationName = $this->$relationName()->getRelated(); |
57
|
|
|
} else { |
58
|
|
|
$relations[] = $parentRelationName->$relationName(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $relations; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the sql for a query with the bindings replaced with the binding values |
67
|
|
|
* |
68
|
|
|
* @param Builder $builder |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function toSqlWithBindings($builder) |
72
|
|
|
{ |
73
|
|
|
$replacements = array_map('addslashes', $builder->getBindings()); |
|
|
|
|
74
|
|
|
$sql = $builder->toSql(); |
75
|
|
|
|
76
|
|
|
foreach ($replacements as &$replacement) { |
77
|
|
|
if (!is_numeric($replacement)) { |
78
|
|
|
$replacement = '"' . $replacement . '"'; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return preg_replace_callback( |
83
|
|
|
'/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/', |
84
|
|
|
function () use (&$replacements) { |
85
|
|
|
return array_shift($replacements); |
86
|
|
|
}, |
87
|
|
|
$sql |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add table name to column name if table name not already included in column name |
93
|
|
|
* |
94
|
|
|
* @param string $table |
95
|
|
|
* @param string $column |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function columnWithTableName($table, $column) |
99
|
|
|
{ |
100
|
|
|
return (preg_match('/(' . $table . '\.|`' . $table . '`)/i', $column) > 0 ? '' : $table . '.') . $column; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get table name from column name |
105
|
|
|
* |
106
|
|
|
* @param string $column |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function getTableFromColumn($column) |
110
|
|
|
{ |
111
|
|
|
$periodPos = strpos($column, '.'); |
112
|
|
|
|
113
|
|
|
return ($periodPos !== false ? substr($column, 0, $periodPos) : $column); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get table name with alias if different to table name |
118
|
|
|
* |
119
|
|
|
* @param \stdClass $table |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function getTableWithAlias($table) |
123
|
|
|
{ |
124
|
|
|
if ($table->alias !== '' && $table->name !== $table->alias) { |
125
|
|
|
return $table->name . ' AS ' . $table->alias; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $table->name; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Remove a global scope if it exists |
133
|
|
|
* |
134
|
|
|
* @param Builder $query |
135
|
|
|
* @param string $scopeName |
136
|
|
|
* @return Builder |
137
|
|
|
*/ |
138
|
|
|
protected function removeGlobalScope($query, $scopeName) |
139
|
|
|
{ |
140
|
|
|
$globalScopes = $this->getGlobalScopes(); |
|
|
|
|
141
|
|
|
if (isset($globalScopes[$scopeName])) { |
142
|
|
|
$query->withoutGlobalScope($scopeName); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $query; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|