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