Completed
Branch feature/pre-split (4c50c1)
by Anton
03:17
created

BelongsToLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureQuery() 0 19 3
A initNode() 0 11 1
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
 * Responsible for loading data related to parent record in belongs to relation. Loading logic is
17
 * identical to HasOneLoader however preferred loading methods is POSTLOAD.
18
 */
19
class BelongsToLoader extends RelationLoader
20
{
21
    /**
22
     * Default set of relation options. Child implementation might defined their of default options.
23
     *
24
     * @var array
25
     */
26
    protected $options = [
27
        'method' => self::POSTLOAD,
28
        'minify' => true,
29
        'alias'  => null,
30
        'using'  => null,
31
        'where'  => null,
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function configureQuery(SelectQuery $query, array $references = []): SelectQuery
38
    {
39
        if ($this->isJoined()) {
40
            $query->join(
41
                $this->getMethod() == self::JOIN ? 'INNER' : 'LEFT',
42
                "{$this->getTable()} AS {$this->getAlias()}",
43
                [$this->localKey(Record::OUTER_KEY) => $this->parentKey(Record::INNER_KEY)]
44
            );
45
        } else {
46
            //This relation is loaded using external query
47
            $query->where(
48
                $this->localKey(Record::OUTER_KEY),
49
                'IN',
50
                new Parameter($references)
51
            );
52
        }
53
54
        return parent::configureQuery($query);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function initNode(): AbstractNode
61
    {
62
        $node = new SinguralNode(
63
            $this->schema[Record::RELATION_COLUMNS],
64
            $this->schema[Record::OUTER_KEY],
65
            $this->schema[Record::INNER_KEY],
66
            $this->schema[Record::SH_PRIMARIES]
67
        );
68
69
        return $node->asJoined($this->isJoined());
70
    }
71
}