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