1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral, Core 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\Nodes\AbstractNode; |
13
|
|
|
use Spiral\ORM\Entities\Nodes\SingularNode; |
14
|
|
|
use Spiral\ORM\Record; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Responsible for loading data related to parent record in belongs to relation. Loading logic is |
18
|
|
|
* identical to HasOneLoader however preferred loading methods is POSTLOAD. |
19
|
|
|
*/ |
20
|
|
|
class BelongsToLoader extends RelationLoader |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Default set of relation options. Child implementation might defined their of default options. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options = [ |
28
|
|
|
'method' => self::POSTLOAD, |
29
|
|
|
'minify' => true, |
30
|
|
|
'alias' => null, |
31
|
|
|
'using' => null, |
32
|
|
|
'where' => null, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery |
39
|
|
|
{ |
40
|
|
|
if (!empty($this->options['using'])) { |
41
|
|
|
//Use pre-defined query |
42
|
|
|
return parent::configureQuery($query, $outerKeys); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->isJoined()) { |
46
|
|
|
$query->join( |
47
|
|
|
$this->getMethod() == self::JOIN ? 'INNER' : 'LEFT', |
48
|
|
|
"{$this->getTable()} AS {$this->getAlias()}") |
49
|
|
|
->on( |
50
|
|
|
$this->localKey(Record::OUTER_KEY), |
51
|
|
|
$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
|
|
|
return parent::configureQuery($query); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function initNode(): AbstractNode |
69
|
|
|
{ |
70
|
|
|
$node = new SingularNode( |
71
|
|
|
$this->schema[Record::RELATION_COLUMNS], |
72
|
|
|
$this->schema[Record::OUTER_KEY], |
73
|
|
|
$this->schema[Record::INNER_KEY], |
74
|
|
|
$this->schema[Record::SH_PRIMARY_KEY] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $node->asJoined($this->isJoined()); |
78
|
|
|
} |
79
|
|
|
} |