TargetQuery::attributesMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\target;
12
13
use hiqdev\billing\hiapi\models\Target;
14
use yii\db\Expression;
15
16
class TargetQuery extends \hiqdev\yii\DataMapper\query\Query
17
{
18
    protected $modelClass = Target::class;
19
20
    /**
21
     * @return array
22
     */
23
    protected function attributesMap()
24
    {
25
        return [
26
            'id' => 'o.obj_id',
27
            'type' => new Expression("
28
                t.name || 
29
                CASE WHEN tt.name IS NOT NULL THEN
30
                    '.' || tt.name
31
                ELSE
32
                    ''
33
                END as type
34
            "),
35
            'name' => new Expression("
36
                coalesce(d.name, tr.name, '') as name
37
            "),
38
        ];
39
    }
40
41
    public function initFrom()
42
    {
43
        return $this->from('obj   o')
44
                ->leftJoin('zref   t',  't.obj_id  = o.class_id')
45
                ->leftJoin('device d',  'd.obj_id  = o.obj_id')
46
                ->leftJoin('tariff tr', 'tr.obj_id = o.obj_id')
47
                ->leftJoin('zref   tt', 'tt.obj_id = coalesce(d.type_id, tr.type_id)');
48
    }
49
}
50