1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spiral\ORM\Entities\Loaders; |
9
|
|
|
|
10
|
|
|
use Spiral\Database\Builders\SelectQuery; |
11
|
|
|
use Spiral\Database\Injections\Parameter; |
12
|
|
|
use Spiral\ORM\Entities\Loaders\Traits\WhereTrait; |
13
|
|
|
use Spiral\ORM\Entities\Nodes\AbstractNode; |
14
|
|
|
use Spiral\ORM\Entities\Nodes\SingularNode; |
15
|
|
|
use Spiral\ORM\ORMInterface; |
16
|
|
|
use Spiral\ORM\Record; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Dedicated to load HAS_ONE relations, by default loader will prefer to join data into query. |
20
|
|
|
* Loader support MORPH_KEY. |
21
|
|
|
* |
22
|
|
|
* Please note that OUTER and INNER keys defined from perspective of parent (reversed for our |
23
|
|
|
* purposes). |
24
|
|
|
*/ |
25
|
|
|
class HasOneLoader extends RelationLoader |
26
|
|
|
{ |
27
|
|
|
use WhereTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Default set of relation options. Child implementation might defined their of default options. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $options = [ |
35
|
|
|
'method' => self::INLOAD, |
36
|
|
|
'minify' => true, |
37
|
|
|
'alias' => null, |
38
|
|
|
'using' => null, |
39
|
|
|
'where' => null, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery |
46
|
|
|
{ |
47
|
|
|
if ($this->isJoined()) { |
48
|
|
|
$query->join( |
49
|
|
|
$this->getMethod() == self::JOIN ? 'INNER' : 'LEFT', |
50
|
|
|
"{$this->getTable()} AS {$this->getAlias()}", |
51
|
|
|
[$this->localKey(Record::OUTER_KEY) => $this->parentKey(Record::INNER_KEY)] |
52
|
|
|
); |
53
|
|
|
} else { |
54
|
|
|
//This relation is loaded using external query |
55
|
|
|
$query->where( |
56
|
|
|
$this->localKey(Record::OUTER_KEY), |
57
|
|
|
'IN', |
58
|
|
|
new Parameter($outerKeys) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//Morphed records |
63
|
|
|
if (!empty($this->schema[Record::MORPH_KEY])) { |
64
|
|
|
$this->setWhere( |
65
|
|
|
$query, |
66
|
|
|
$this->getAlias(), |
67
|
|
|
$this->isJoined() ? 'onWhere' : 'where', |
68
|
|
|
[ |
69
|
|
|
$this->localKey(Record::MORPH_KEY) => $this->orm->define( |
70
|
|
|
$this->parent->getClass(), |
71
|
|
|
ORMInterface::R_ROLE_NAME |
72
|
|
|
) |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return parent::configureQuery($query); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
protected function initNode(): AbstractNode |
84
|
|
|
{ |
85
|
|
|
$node = new SingularNode( |
86
|
|
|
$this->schema[Record::RELATION_COLUMNS], |
87
|
|
|
$this->schema[Record::OUTER_KEY], |
88
|
|
|
$this->schema[Record::INNER_KEY], |
89
|
|
|
$this->schema[Record::SH_PRIMARY_KEY] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return $node->asJoined($this->isJoined()); |
93
|
|
|
} |
94
|
|
|
} |