|
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 Order. |
|
12
|
|
|
* |
|
13
|
|
|
* @property int $id |
|
14
|
|
|
* @property int $customer_id |
|
15
|
|
|
* @property int $created_at |
|
16
|
|
|
* @property string $total |
|
17
|
|
|
*/ |
|
18
|
|
|
class Order extends ActiveRecord |
|
19
|
|
|
{ |
|
20
|
|
|
public const TABLE_NAME = 'order'; |
|
21
|
|
|
|
|
22
|
|
|
public function getTableName(): string |
|
23
|
|
|
{ |
|
24
|
|
|
return self::TABLE_NAME; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getCustomer(): ActiveQuery |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->hasOne(Customer::class, ['id' => 'customer_id']); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getCustomerJoinedWithProfile(): ActiveQuery |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->hasOne(Customer::class, ['id' => 'customer_id'])->joinWith('profile'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getCustomerJoinedWithProfileIndexOrdered(): ActiveQuery |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->hasMany( |
|
40
|
|
|
Customer::class, |
|
41
|
|
|
['id' => 'customer_id'] |
|
42
|
|
|
)->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getCustomer2(): ActiveQuery |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders2'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getOrderItems(): ActiveQuery |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->hasMany(OrderItem::class, ['order_id' => 'id']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getOrderItems2(): ActiveQuery |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->hasMany(OrderItem::class, ['order_id' => 'id'])->indexBy('item_id'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getOrderItems3(): ActiveQuery |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->hasMany( |
|
63
|
|
|
OrderItem::class, |
|
64
|
|
|
['order_id' => 'id'] |
|
65
|
|
|
)->indexBy(fn ($row) => $row['order_id'] . '_' . $row['item_id']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getOrderItemsWithNullFK(): ActiveQuery |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getItems(): ActiveQuery |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
|
|
|
|
|
|
76
|
|
|
// additional query configuration |
|
77
|
|
|
})->orderBy('item.id'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getItemsIndexed(): ActiveQuery |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->indexBy('id'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getItemsWithNullFK(): ActiveQuery |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->hasMany( |
|
88
|
|
|
Item::class, |
|
89
|
|
|
['id' => 'item_id'] |
|
90
|
|
|
)->viaTable('order_item_with_null_fk', ['order_id' => 'id']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getItemsInOrder1(): ActiveQuery |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
|
96
|
|
|
$q->orderBy(['subtotal' => SORT_ASC]); |
|
97
|
|
|
})->orderBy('name'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getItemsInOrder2(): ActiveQuery |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
|
103
|
|
|
$q->orderBy(['subtotal' => SORT_DESC]); |
|
104
|
|
|
})->orderBy('name'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getBooks(): ActiveQuery |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->where(['category_id' => 1]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getBooksWithNullFK(): ActiveQuery |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->hasMany( |
|
115
|
|
|
Item::class, |
|
116
|
|
|
['id' => 'item_id'] |
|
117
|
|
|
)->via('orderItemsWithNullFK')->where(['category_id' => 1]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getBooksViaTable(): ActiveQuery |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->hasMany( |
|
123
|
|
|
Item::class, |
|
124
|
|
|
['id' => 'item_id'] |
|
125
|
|
|
)->viaTable('order_item', ['order_id' => 'id'])->where(['category_id' => 1]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getBooksWithNullFKViaTable(): ActiveQuery |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->hasMany( |
|
131
|
|
|
Item::class, |
|
132
|
|
|
['id' => 'item_id'] |
|
133
|
|
|
)->viaTable('order_item_with_null_fk', ['order_id' => 'id'])->where(['category_id' => 1]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getBooks2(): ActiveQuery |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->hasMany( |
|
139
|
|
|
Item::class, |
|
140
|
|
|
['id' => 'item_id'] |
|
141
|
|
|
)->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getBooksExplicit(): ActiveQuery |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->hasMany( |
|
147
|
|
|
Item::class, |
|
148
|
|
|
['id' => 'item_id'] |
|
149
|
|
|
)->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getBooksExplicitA(): ActiveQuery |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->hasMany( |
|
155
|
|
|
Item::class, |
|
156
|
|
|
['id' => 'item_id'] |
|
157
|
|
|
)->alias('bo')->onCondition(['bo.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getBookItems(): ActiveQuery |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->hasMany( |
|
163
|
|
|
Item::class, |
|
164
|
|
|
['id' => 'item_id'] |
|
165
|
|
|
)->alias('books')->onCondition(['books.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function getMovieItems(): ActiveQuery |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->hasMany( |
|
171
|
|
|
Item::class, |
|
172
|
|
|
['id' => 'item_id'] |
|
173
|
|
|
)->alias('movies')->onCondition(['movies.category_id' => 2])->viaTable('order_item', ['order_id' => 'id']); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getLimitedItems(): ActiveQuery |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->onCondition(['item.id' => [3, 5]])->via('orderItems'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function getExpensiveItemsUsingViaWithCallable(): ActiveQuery |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) { |
|
184
|
|
|
$q->where(['>=', 'subtotal', 10]); |
|
185
|
|
|
}); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function getCheapItemsUsingViaWithCallable(): ActiveQuery |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) { |
|
191
|
|
|
$q->where(['<', 'subtotal', 10]); |
|
192
|
|
|
}); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function activeAttributes(): array |
|
196
|
|
|
{ |
|
197
|
|
|
return [ |
|
198
|
|
|
0 => 'customer_id', |
|
199
|
|
|
]; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function getOrderItemsFor8() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id'])->andOnCondition(['subtotal' => 8.0]); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getItemsFor8() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItemsFor8'); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.