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