Passed
Push — master ( 8c0a7b...a4816d )
by Sergei
02:51
created

OrderItem::getCustom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\ActiveRecord;
9
10
/**
11
 * Class OrderItem.
12
 *
13
 * @property int $order_id
14
 * @property int $item_id
15
 * @property int $quantity
16
 * @property string $subtotal
17
 */
18
final class OrderItem extends ActiveRecord
19
{
20
    public function getTableName(): string
21
    {
22
        return 'order_item';
23
    }
24
25
    public function fields(): array
26
    {
27
        $fields = parent::fields();
28
29
        $fields['order_id'] = $this->getAttribute('order_id');
30
        $fields['item_id'] = $this->getAttribute('item_id');
31
        $fields['price'] = $this->getAttribute('subtotal') / $this->getAttribute('quantity');
32
        $fields['quantity'] = $this->getAttribute('quantity');
33
        $fields['subtotal'] = $this->getAttribute('subtotal');
34
35
        return $fields;
36
    }
37
38
    public function getOrderQuery(): ActiveQuery
39
    {
40
        return $this->hasOne(Order::class, ['id' => 'order_id']);
41
    }
42
43
    public function getItemQuery(): ActiveQuery
44
    {
45
        return $this->hasOne(Item::class, ['id' => 'item_id']);
46
    }
47
48
    public function getOrderItemCompositeWithJoinQuery(): ActiveQuery
49
    {
50
        /** relations used by testFindCompositeWithJoin() */
51
        return $this->hasOne(self::class, ['item_id' => 'item_id', 'order_id' => 'order_id' ])->joinWith('item');
52
    }
53
54
    public function getOrderItemCompositeNoJoinQuery(): ActiveQuery
55
    {
56
        return $this->hasOne(self::class, ['item_id' => 'item_id', 'order_id' => 'order_id' ]);
57
    }
58
59
    public function getCustomQuery(): ActiveQuery
60
    {
61
        return new ActiveQuery(Order::class, $this->db);
62
    }
63
}
64