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 Customer. |
12
|
|
|
* |
13
|
|
|
* @property int $id |
14
|
|
|
* @property string $name |
15
|
|
|
* @property string $email |
16
|
|
|
* @property string $address |
17
|
|
|
* @property int $status |
18
|
|
|
* |
19
|
|
|
* @method CustomerQuery findBySql($sql, $params = []) static. |
20
|
|
|
*/ |
21
|
|
|
class Customer extends ActiveRecord |
22
|
|
|
{ |
23
|
|
|
public const STATUS_ACTIVE = 1; |
24
|
|
|
public const STATUS_INACTIVE = 2; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|string |
28
|
|
|
*/ |
29
|
|
|
public $status2; |
30
|
|
|
/** |
31
|
|
|
* @var int|string|null |
32
|
|
|
*/ |
33
|
|
|
public $sumTotal; |
34
|
|
|
|
35
|
|
|
public function getTableName(): string |
36
|
|
|
{ |
37
|
|
|
return 'customer'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getName(): string |
41
|
|
|
{ |
42
|
|
|
return $this->getAttribute('name'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getProfileQuery(): ActiveQuery |
46
|
|
|
{ |
47
|
|
|
return $this->hasOne(Profile::class, ['id' => 'profile_id']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getOrdersPlainQuery(): ActiveQuery |
51
|
|
|
{ |
52
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getOrdersQuery(): ActiveQuery |
56
|
|
|
{ |
57
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('[[id]]'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getOrdersNoOrderQuery(): ActiveQuery |
61
|
|
|
{ |
62
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getExpensiveOrdersQuery(): ActiveQuery |
66
|
|
|
{ |
67
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getItemQuery(): void |
71
|
|
|
{ |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getOrdersWithItemsQuery(): ActiveQuery |
75
|
|
|
{ |
76
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id'])->with('orderItems'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getExpensiveOrdersWithNullFKQuery(): ActiveQuery |
80
|
|
|
{ |
81
|
|
|
return $this->hasMany( |
82
|
|
|
OrderWithNullFK::class, |
83
|
|
|
['customer_id' => 'id'] |
84
|
|
|
)->andWhere('[[total]] > 50')->orderBy('id'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getOrdersWithNullFKQuery(): ActiveQuery |
88
|
|
|
{ |
89
|
|
|
return $this->hasMany(OrderWithNullFK::class, ['customer_id' => 'id'])->orderBy('id'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getOrders2Query(): ActiveQuery |
93
|
|
|
{ |
94
|
|
|
return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** deeply nested table relation */ |
98
|
|
|
public function getOrderItemsQuery(): ActiveQuery |
99
|
|
|
{ |
100
|
|
|
$rel = $this->hasMany(Item::class, ['id' => 'item_id']); |
101
|
|
|
|
102
|
|
|
return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) { |
103
|
|
|
/* @var $q ActiveQuery */ |
104
|
|
|
$q->viaTable('order', ['customer_id' => 'id']); |
105
|
|
|
})->orderBy('id'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setOrdersReadOnly(): void |
109
|
|
|
{ |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getOrderItems2Query(): ActiveQuery |
113
|
|
|
{ |
114
|
|
|
return $this->hasMany(OrderItem::class, ['order_id' => 'id']) |
115
|
|
|
->via('ordersNoOrder'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getItems2Query(): ActiveQuery |
119
|
|
|
{ |
120
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id']) |
121
|
|
|
->via('orderItems2'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|