|
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 getOrder(): ActiveQuery |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->hasOne(Order::class, ['id' => 'order_id']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getItem(): ActiveQuery |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->hasOne(Item::class, ['id' => 'item_id']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getOrderItemCompositeWithJoin(): 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 getOrderItemCompositeNoJoin(): ActiveQuery |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->hasOne(self::class, ['item_id' => 'item_id', 'order_id' => 'order_id' ]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getCustom(): ActiveQuery |
|
60
|
|
|
{ |
|
61
|
|
|
return new ActiveQuery(Order::class, $this->db); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|