|
1
|
|
|
<?php |
|
2
|
|
|
namespace sonrac\Arango\Eloquent; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use sonrac\Arango\Eloquent\Reletations\BelongsTo; |
|
7
|
|
|
use sonrac\Arango\Eloquent\Reletations\BelongsToMany; |
|
8
|
|
|
use function sonrac\Arango\Helpers\getEntityName; |
|
9
|
|
|
use sonrac\Arango\Query\QueryBuilder; |
|
10
|
|
|
use \Illuminate\Database\Eloquent\Builder as BaseBuilder; |
|
11
|
|
|
|
|
12
|
|
|
abstract class Model extends BaseModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @inheritdoc |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $primaryKey = '_key'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $keyType = 'string'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get collection name |
|
26
|
|
|
* |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
function getCollection(){ |
|
|
|
|
|
|
30
|
|
|
return $this->getTable(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set collection name |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $collection |
|
37
|
|
|
*/ |
|
38
|
|
|
function setCollection($collection){ |
|
|
|
|
|
|
39
|
|
|
$this->table = $collection; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function newBaseQueryBuilder() |
|
46
|
|
|
{ |
|
47
|
|
|
$connection = $this->getConnection(); |
|
48
|
|
|
|
|
49
|
|
|
return new QueryBuilder( |
|
50
|
|
|
$connection, $connection->getQueryGrammar(), $connection->getPostProcessor() |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function qualifyColumn($column) |
|
58
|
|
|
{ |
|
59
|
|
|
if (Str::contains($column, '.')) { |
|
60
|
|
|
return $column; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->getEntityName().'.'.$column; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getEntityName(){ |
|
67
|
|
|
return getEntityName($this->getCollection()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
|
|
public function newFromBuilder($attributes = [], $connection = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$model = $this->newInstance([], true); |
|
76
|
|
|
|
|
77
|
|
|
$attributesResult = []; |
|
78
|
|
|
foreach ($attributes as $key => $value){ |
|
79
|
|
|
if(is_array($value) && $this->getEntityName() === $key){ |
|
80
|
|
|
|
|
81
|
|
|
$attributesResult = array_merge($attributesResult, $value); |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
$attributesResult[$key] = $value; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$model->setRawAttributes((array) $attributesResult, true); |
|
88
|
|
|
|
|
89
|
|
|
$model->setConnection($connection ?: $this->getConnectionName()); |
|
90
|
|
|
|
|
91
|
|
|
$model->fireModelEvent('retrieved', false); |
|
92
|
|
|
|
|
93
|
|
|
return $model; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public function hasOne($related, $foreignKey = null, $localKey = null) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$instance = $this->newRelatedInstance($related); |
|
102
|
|
|
|
|
103
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
104
|
|
|
|
|
105
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
|
106
|
|
|
|
|
107
|
|
|
return $this->newHasOne($instance->newQuery(), $this, $foreignKey, $localKey); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritdoc |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function hasMany($related, $foreignKey = null, $localKey = null) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$instance = $this->newRelatedInstance($related); |
|
116
|
|
|
|
|
117
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
118
|
|
|
|
|
119
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
|
120
|
|
|
|
|
121
|
|
|
return $this->newHasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
function newBelongsTo(BaseBuilder $query, BaseModel $child, $foreignKey, $ownerKey, $relation) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @inheritdoc |
|
134
|
|
|
*/ |
|
135
|
|
|
function newBelongsToMany(BaseBuilder $query, BaseModel $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.