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
|
|
|
* @override |
26
|
|
|
* Create a new Eloquent query builder for the model. |
27
|
|
|
* |
28
|
|
|
* @param QueryBuilder $query |
29
|
|
|
* |
30
|
|
|
* @return Builder |
31
|
|
|
*/ |
32
|
83 |
|
public function newEloquentBuilder($query) |
33
|
|
|
{ |
34
|
83 |
|
return new Builder($query); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get a new query builder instance for the connection. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Database\Query\Builder |
41
|
|
|
*/ |
42
|
83 |
|
protected function newBaseQueryBuilder() |
43
|
|
|
{ |
44
|
83 |
|
return $this->getConnection()->query(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Dynamically retrieve attributes on the model. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
45 |
|
public function __get($key) |
54
|
|
|
{ |
55
|
|
|
// Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here. |
56
|
45 |
|
if ($key === 'id') { |
57
|
2 |
|
return $this->attributes['_id']; |
58
|
|
|
} |
59
|
44 |
|
return $this->getAttribute($key); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Dynamically set attributes on the model. |
64
|
|
|
* |
65
|
|
|
* @param string $key |
66
|
|
|
* @param mixed $value |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
17 |
|
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
|
17 |
|
if ($key === 'id') { |
73
|
1 |
|
$this->attributes['_id'] = $value; |
|
|
|
|
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
16 |
|
$this->setAttribute($key, $value); |
|
|
|
|
78
|
16 |
|
} |
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
|
10 |
|
public function setKeyAttribute($value) |
88
|
|
|
{ |
89
|
10 |
|
$this->attributes['_key'] = $value; |
|
|
|
|
90
|
|
|
|
91
|
10 |
|
$this->updateIdWithKey($value); |
92
|
10 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $key |
96
|
|
|
*/ |
97
|
10 |
|
protected function updateIdWithKey(string $key) |
98
|
|
|
{ |
99
|
10 |
|
if (! isset($this->attributes['_id'])) { |
100
|
9 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']); |
104
|
1 |
|
$this->attributes['_id'] = $id; |
|
|
|
|
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Qualify the given column name by the model's table. |
109
|
|
|
* |
110
|
|
|
* @param string $column |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
39 |
|
public function qualifyColumn($column) |
115
|
|
|
{ |
116
|
39 |
|
$tableReferer = Str::singular($this->getTable()) . 'Doc'; |
|
|
|
|
117
|
|
|
|
118
|
39 |
|
if (Str::startsWith($column, $tableReferer . '.')) { |
119
|
|
|
return $column; |
120
|
|
|
} |
121
|
|
|
|
122
|
39 |
|
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
|
|
|
// if ($keyName[0] != '_') { |
135
|
|
|
// $keyName = '_' . $keyName; |
136
|
|
|
// } |
137
|
|
|
|
138
|
8 |
|
return Str::snake(class_basename($this)) . $keyName; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
protected function execute(ArangoQueryBuilder $aqb) |
142
|
|
|
{ |
143
|
1 |
|
$connection = $this->getConnection(); |
144
|
1 |
|
$results = $connection->execute($aqb->get()); |
145
|
1 |
|
return $this->hydrate($results); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|