1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Builder; |
7
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder; |
8
|
|
|
|
9
|
|
|
trait IsAranguentModel |
10
|
|
|
{ |
11
|
|
|
use HasAranguentRelationships; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @override |
15
|
|
|
* Create a new Eloquent query builder for the model. |
16
|
|
|
* |
17
|
|
|
* @param QueryBuilder $query |
18
|
|
|
* |
19
|
|
|
* @return Builder |
20
|
|
|
*/ |
21
|
79 |
|
public function newEloquentBuilder($query) |
22
|
|
|
{ |
23
|
79 |
|
return new Builder($query); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get a new query builder instance for the connection. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Database\Query\Builder |
30
|
|
|
*/ |
31
|
79 |
|
protected function newBaseQueryBuilder() |
32
|
|
|
{ |
33
|
79 |
|
return $this->getConnection()->query(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Dynamically retrieve attributes on the model. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
46 |
|
public function __get($key) |
43
|
|
|
{ |
44
|
|
|
// Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here. |
45
|
46 |
|
if ($key === '_id') { |
46
|
10 |
|
return $this->attributes['_id']; |
47
|
|
|
} |
48
|
44 |
|
return $this->getAttribute($key); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Map the id attribute commonly used in Laravel to the primary key for third-party compatibility. |
53
|
|
|
* In ArangoDB '_key' is the equivalent of 'id' in sql databases. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
1 |
|
public function getIdAttribute() |
58
|
|
|
{ |
59
|
1 |
|
return $this->{$this->primaryKey}; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Dynamically set attributes on the model. |
64
|
|
|
* |
65
|
|
|
* @param string $key |
66
|
|
|
* @param mixed $value |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
18 |
|
public function __set($key, $value) |
70
|
|
|
{ |
71
|
|
|
// Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here. |
72
|
18 |
|
if ($key === '_id') { |
73
|
1 |
|
$this->attributes[$key] = $value; |
|
|
|
|
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
17 |
|
$this->setAttribute($key, $value); |
|
|
|
|
78
|
17 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Map the id attribute commonly used in Laravel to the primary key for third-party compatibility. |
82
|
|
|
* In ArangoDB '_key' is the equivalent of 'id' in sql databases. |
83
|
|
|
* |
84
|
|
|
* @param string $value |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
1 |
|
public function setIdAttribute($value) |
88
|
|
|
{ |
89
|
1 |
|
$this->attributes[$this->primaryKey] = $value; |
|
|
|
|
90
|
1 |
|
if ($this->primaryKey === '_key') { |
91
|
1 |
|
$this->updateIdWithKey($value); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Map the id attribute commonly used in Laravel to the primary key for third-party compatibility. |
97
|
|
|
* In ArangoDB '_key' is the equivalent of 'id' in sql databases. |
98
|
|
|
* |
99
|
|
|
* @param string $value |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
10 |
|
public function setKeyAttribute($value) |
103
|
|
|
{ |
104
|
10 |
|
$this->attributes['_key'] = $value; |
|
|
|
|
105
|
|
|
|
106
|
10 |
|
$this->updateIdWithKey($value); |
107
|
10 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $key |
111
|
|
|
*/ |
112
|
11 |
|
protected function updateIdWithKey(string $key) |
113
|
|
|
{ |
114
|
11 |
|
if (! isset($this->attributes['_id'])) { |
115
|
9 |
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$id = preg_replace ( "/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']); |
119
|
2 |
|
$this->attributes['_id'] = $id; |
|
|
|
|
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Qualify the given column name by the model's table. |
124
|
|
|
* |
125
|
|
|
* @param string $column |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
39 |
|
public function qualifyColumn($column) |
130
|
|
|
{ |
131
|
39 |
|
$tableReferer = Str::singular($this->getTable()) . 'Doc'; |
|
|
|
|
132
|
|
|
|
133
|
39 |
|
if (Str::startsWith($column, $tableReferer . '.')) { |
134
|
|
|
return $column; |
135
|
|
|
} |
136
|
|
|
|
137
|
39 |
|
return $tableReferer . '.' . $column; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the default foreign key name for the model. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
8 |
|
public function getForeignKey() |
146
|
|
|
{ |
147
|
8 |
|
$keyName = $this->getKeyName(); |
|
|
|
|
148
|
|
|
|
149
|
8 |
|
if ($keyName[0] != '_') { |
150
|
|
|
$keyName = '_' . $keyName; |
151
|
|
|
} |
152
|
|
|
|
153
|
8 |
|
return Str::snake(class_basename($this)) . $keyName; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|