|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Select\Loader; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\Query\SelectQuery; |
|
8
|
|
|
use Cycle\ORM\FactoryInterface; |
|
9
|
|
|
use Cycle\ORM\Parser\AbstractNode; |
|
10
|
|
|
use Cycle\ORM\Parser\SingularNode; |
|
11
|
|
|
use Cycle\ORM\Service\SourceProviderInterface; |
|
12
|
|
|
use Cycle\ORM\Relation; |
|
13
|
|
|
use Cycle\ORM\SchemaInterface; |
|
14
|
|
|
use Cycle\ORM\Select\JoinableLoader; |
|
15
|
|
|
use Cycle\ORM\Select\Traits\JoinOneTableTrait; |
|
16
|
|
|
use Cycle\ORM\Select\Traits\OrderByTrait; |
|
17
|
|
|
use Cycle\ORM\Select\Traits\WhereTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Dedicated to load HAS_ONE relations, by default loader will prefer to join data into query. |
|
21
|
|
|
* Loader support MORPH_KEY. |
|
22
|
|
|
* |
|
23
|
|
|
* Please note that OUTER and INNER keys defined from perspective of parent (reversed for our |
|
24
|
|
|
* purposes). |
|
25
|
|
|
* |
|
26
|
|
|
* @internal |
|
27
|
|
|
*/ |
|
28
|
|
|
class HasOneLoader extends JoinableLoader |
|
29
|
|
|
{ |
|
30
|
|
|
use JoinOneTableTrait; |
|
31
|
|
|
use OrderByTrait; |
|
32
|
|
|
use WhereTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Default set of relation options. Child implementation might defined their of default options. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected array $options = [ |
|
38
|
|
|
'load' => false, |
|
39
|
|
|
'scope' => true, |
|
40
|
|
|
'method' => self::INLOAD, |
|
41
|
|
|
'minify' => true, |
|
42
|
|
|
'as' => null, |
|
43
|
722 |
|
'using' => null, |
|
44
|
|
|
'where' => null, |
|
45
|
722 |
|
'orderBy' => null, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct( |
|
49
|
|
|
SchemaInterface $ormSchema, |
|
50
|
722 |
|
SourceProviderInterface $sourceProvider, |
|
51
|
|
|
FactoryInterface $factory, |
|
52
|
|
|
string $name, |
|
53
|
722 |
|
string $target, |
|
54
|
|
|
array $schema |
|
55
|
722 |
|
) { |
|
56
|
722 |
|
parent::__construct($ormSchema, $sourceProvider, $factory, $name, $target, $schema); |
|
57
|
|
|
$this->options['where'] = $schema[Relation::WHERE] ?? []; |
|
58
|
|
|
$this->options['orderBy'] = $schema[Relation::ORDER_BY] ?? []; |
|
59
|
722 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery |
|
62
|
634 |
|
{ |
|
63
|
|
|
if ($this->options['using'] !== null) { |
|
64
|
634 |
|
// use pre-defined query |
|
65
|
634 |
|
return parent::configureQuery($query, $outerKeys); |
|
66
|
634 |
|
} |
|
67
|
634 |
|
|
|
68
|
634 |
|
$this->configureParentQuery($query, $outerKeys); |
|
69
|
|
|
|
|
70
|
|
|
// user specified WHERE conditions |
|
71
|
|
|
$this->setWhere( |
|
72
|
|
|
$query, |
|
73
|
|
|
$this->isJoined() ? 'onWhere' : 'where', |
|
74
|
|
|
$this->options['where'] ?? $this->schema[Relation::WHERE] ?? [] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
// user specified ORDER_BY rules |
|
78
|
|
|
$this->setOrderBy( |
|
79
|
|
|
$query, |
|
80
|
|
|
$this->getAlias(), |
|
81
|
|
|
$this->options['orderBy'] ?? $this->schema[Relation::ORDER_BY] ?? [] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return parent::configureQuery($query); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function initNode(): AbstractNode |
|
88
|
|
|
{ |
|
89
|
|
|
return new SingularNode( |
|
90
|
|
|
$this->columnNames(), |
|
91
|
|
|
(array)$this->define(SchemaInterface::PRIMARY_KEY), |
|
92
|
|
|
(array)$this->schema[Relation::OUTER_KEY], |
|
93
|
|
|
(array)$this->schema[Relation::INNER_KEY] |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|