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