|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bakery\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
|
6
|
|
|
use Bakery\Eloquent\ModelSchema; |
|
7
|
|
|
use Bakery\Support\TypeRegistry; |
|
8
|
|
|
use Bakery\Traits\JoinsRelationships; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
use Bakery\Queries\Concerns\EagerLoadRelationships; |
|
11
|
|
|
|
|
12
|
|
|
abstract class EloquentQuery extends Query |
|
13
|
|
|
{ |
|
14
|
|
|
use JoinsRelationships; |
|
15
|
|
|
use EagerLoadRelationships; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Bakery\Eloquent\ModelSchema |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $modelSchema; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* EloquentQuery constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Bakery\Support\TypeRegistry $registry |
|
31
|
|
|
* @param \Bakery\Eloquent\ModelSchema $modelSchema |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(TypeRegistry $registry, ModelSchema $modelSchema = null) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($registry); |
|
36
|
|
|
|
|
37
|
|
|
if ($modelSchema) { |
|
38
|
|
|
$this->modelSchema = $modelSchema; |
|
39
|
|
|
} elseif (is_string($this->modelSchema)) { |
|
|
|
|
|
|
40
|
|
|
$this->modelSchema = $this->registry->getModelSchema($this->modelSchema); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
Utils::invariant( |
|
44
|
|
|
$this->modelSchema instanceof ModelSchema, |
|
45
|
|
|
'Model schema on '.get_class($this).' should be an instance of '.ModelSchema::class |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$this->model = $this->modelSchema->getModel(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the model schema. |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Bakery\Eloquent\ModelSchema |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getModelSchema(): ModelSchema |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->modelSchema; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Scope the query. |
|
63
|
|
|
* This can be overwritten to make your own collection queries. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Builder $query |
|
66
|
|
|
* @return Builder |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function scopeQuery(Builder $query): Builder |
|
69
|
|
|
{ |
|
70
|
|
|
return $query; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|