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