|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace duxet\Rethinkdb\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use duxet\Rethinkdb\Eloquent\Relations\BelongsTo; |
|
8
|
|
|
use duxet\Rethinkdb\Query\Builder as QueryBuilder; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
11
|
|
|
|
|
12
|
|
|
class Model extends \Illuminate\Database\Eloquent\Model |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Get the format for database stored dates. |
|
16
|
|
|
* |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function getDateFormat() |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->dateFormat ?: 'Y-m-d H:i:s'; |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get the table qualified key name. |
|
26
|
|
|
* |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getQualifiedKeyName() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getKeyName(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Ensure Timestamps are returned in DateTime. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \DateTime $value |
|
38
|
|
|
* |
|
39
|
|
|
* @return \DateTime |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function asDateTime($value) |
|
42
|
|
|
{ |
|
43
|
|
|
// Legacy support for Laravel 5.0 |
|
44
|
|
|
if (!$value instanceof Carbon) { |
|
45
|
|
|
return Carbon::instance($value); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return parent::asDateTime($value); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Retain DateTime format for storage. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \DateTime $value |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function fromDateTime($value) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($value instanceof DateTime) { |
|
61
|
|
|
return $value; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return parent::asDateTime($value); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get a new query builder instance for the connection. |
|
69
|
|
|
* |
|
70
|
|
|
* @return Builder |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function newBaseQueryBuilder() |
|
73
|
|
|
{ |
|
74
|
|
|
$connection = $this->getConnection(); |
|
75
|
|
|
// Check the connection type |
|
76
|
|
|
if ($connection instanceof \duxet\Rethinkdb\Connection) { |
|
77
|
|
|
return new QueryBuilder($connection); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return parent::newBaseQueryBuilder(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create a new Eloquent query builder for the model. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \duxet\Rethinkdb\Query\Builder $query |
|
87
|
|
|
* |
|
88
|
|
|
* @return \duxet\Rethinkdb\Eloquent\Builder|static |
|
89
|
|
|
*/ |
|
90
|
|
|
public function newEloquentBuilder($query) |
|
91
|
|
|
{ |
|
92
|
|
|
return new Builder($query); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Define an inverse one-to-one or many relationship. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $related |
|
99
|
|
|
* @param string $foreignKey |
|
100
|
|
|
* @param string $otherKey |
|
101
|
|
|
* @param string $relation |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
104
|
|
|
*/ |
|
105
|
|
|
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) |
|
106
|
|
|
{ |
|
107
|
|
|
// If no relation name was given, we will use this debug backtrace to extract |
|
108
|
|
|
// the calling method's name and use that as the relationship name as most |
|
109
|
|
|
// of the time this will be what we desire to use for the relationships. |
|
110
|
|
|
if (is_null($relation)) { |
|
111
|
|
|
list(, $caller) = debug_backtrace(false, 2); |
|
112
|
|
|
$relation = $caller['function']; |
|
113
|
|
|
} |
|
114
|
|
|
// If no foreign key was supplied, we can use a backtrace to guess the proper |
|
115
|
|
|
// foreign key name by using the name of the relationship function, which |
|
116
|
|
|
// when combined with an "_id" should conventionally match the columns. |
|
117
|
|
|
if (is_null($foreignKey)) { |
|
118
|
|
|
$foreignKey = snake_case($relation).'_id'; |
|
119
|
|
|
} |
|
120
|
|
|
$instance = new $related(); |
|
121
|
|
|
// Once we have the foreign key names, we'll just create a new Eloquent query |
|
122
|
|
|
// for the related models and returns the relationship instance which will |
|
123
|
|
|
// actually be responsible for retrieving and hydrating every relations. |
|
124
|
|
|
$query = $instance->newQuery(); |
|
125
|
|
|
$otherKey = $otherKey ?: $instance->getKeyName(); |
|
126
|
|
|
|
|
127
|
|
|
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Define a one-to-one relationship. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $related |
|
134
|
|
|
* @param string $foreignKey |
|
135
|
|
|
* @param string $localKey |
|
136
|
|
|
* |
|
137
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
public function hasOne($related, $foreignKey = null, $localKey = null) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
142
|
|
|
$instance = new $related(); |
|
143
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
|
144
|
|
|
|
|
145
|
|
|
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Define a one-to-many relationship. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $related |
|
152
|
|
|
* @param string $foreignKey |
|
153
|
|
|
* @param string $localKey |
|
154
|
|
|
* |
|
155
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function hasMany($related, $foreignKey = null, $localKey = null) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
160
|
|
|
$instance = new $related(); |
|
161
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
|
162
|
|
|
|
|
163
|
|
|
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Determine if the new and old values for a given key are numerically equivalent. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $key |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function originalIsNumericallyEquivalent($key) |
|
174
|
|
|
{ |
|
175
|
|
|
$current = $this->attributes[$key]; |
|
176
|
|
|
$original = $this->original[$key]; |
|
177
|
|
|
// Date comparison. |
|
178
|
|
|
if (in_array($key, $this->getDates())) { |
|
179
|
|
|
$current = $current instanceof DateTime ? $this->asDateTime($current) : $current; |
|
180
|
|
|
$original = $original instanceof DateTime ? $this->asDateTime($original) : $original; |
|
181
|
|
|
|
|
182
|
|
|
return $current == $original; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return parent::originalIsNumericallyEquivalent($key); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.