MetaAssociationsQuery::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\meta\db;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\craft\sortable\associations\db\SortableAssociationQuery;
14
use flipbox\meta\records\Meta as MetaRecord;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 *
20
 * @method MetaRecord one($db = null)
21
 * @method MetaRecord[] all($db = null)
22
 * @method MetaRecord[] getCachedResult($db = null)
23
 */
24
class MetaAssociationsQuery extends SortableAssociationQuery
25
{
26
    use traits\Attributes;
27
28
    /**
29
     * @var int|int[]|false|null The Id(s). Prefix Ids with "not " to exclude them.
30
     */
31
    public $id;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function init()
37
    {
38
        parent::init();
39
40
        if ($this->from === null) {
41
            $this->from([
42
                MetaRecord::tableName() . ' ' . MetaRecord::tableAlias()
43
            ]);
44
        }
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected function fixedOrderColumn(): string
51
    {
52
        return 'id';
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function prepare($builder)
59
    {
60
        if (($this->ownerId !== null && empty($this->ownerId)) ||
61
            ($this->id !== null && empty($this->id))
62
        ) {
63
            throw new QueryAbortedException();
64
        }
65
66
        if ($this->id !== null) {
67
            $this->andWhere(Db::parseParam('id', $this->id));
68
        }
69
70
        $this->applyConditions($this);
71
72
        return parent::prepare($builder);
73
    }
74
}
75