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

Order::getBooksViaTableQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
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 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
    private string|int|null $virtualCustomerId = null;
23
24
    public function getTableName(): string
25
    {
26
        return self::TABLE_NAME;
27
    }
28
29
    public function setVirtualCustomerId(string|int|null $virtualCustomerId = null): void
30
    {
31
        $this->virtualCustomerId = $virtualCustomerId;
32
    }
33
34
    public function getVirtualCustomerQuery()
35
    {
36
        return $this->hasOne(Customer::class, ['id' => 'virtualCustomerId']);
37
    }
38
39
    public function getCustomerQuery(): ActiveQuery
40
    {
41
        return $this->hasOne(Customer::class, ['id' => 'customer_id']);
42
    }
43
44
    public function getCustomerJoinedWithProfileQuery(): ActiveQuery
45
    {
46
        return $this->hasOne(Customer::class, ['id' => 'customer_id'])->joinWith('profile');
47
    }
48
49
    public function getCustomerJoinedWithProfileIndexOrderedQuery(): ActiveQuery
50
    {
51
        return $this->hasMany(
52
            Customer::class,
53
            ['id' => 'customer_id']
54
        )->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name');
55
    }
56
57
    public function getCustomer2Query(): ActiveQuery
58
    {
59
        return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders2');
60
    }
61
62
    public function getOrderItemsQuery(): ActiveQuery
63
    {
64
        return $this->hasMany(OrderItem::class, ['order_id' => 'id']);
65
    }
66
67
    public function getOrderItems2Query(): ActiveQuery
68
    {
69
        return $this->hasMany(OrderItem::class, ['order_id' => 'id'])->indexBy('item_id');
70
    }
71
72
    public function getOrderItems3Query(): ActiveQuery
73
    {
74
        return $this->hasMany(
75
            OrderItem::class,
76
            ['order_id' => 'id']
77
        )->indexBy(fn ($row) => $row['order_id'] . '_' . $row['item_id']);
78
    }
79
80
    public function getOrderItemsWithNullFKQuery(): ActiveQuery
81
    {
82
        return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id']);
83
    }
84
85
    public function getItemsQuery(): ActiveQuery
86
    {
87
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) {
0 ignored issues
show
Unused Code introduced by
The parameter $q is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function (/** @scrutinizer ignore-unused */ $q) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
            // additional query configuration
89
        })->orderBy('item.id');
90
    }
91
92
    public function getItemsIndexedQuery(): ActiveQuery
93
    {
94
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->indexBy('id');
95
    }
96
97
    public function getItemsWithNullFKQuery(): ActiveQuery
98
    {
99
        return $this->hasMany(
100
            Item::class,
101
            ['id' => 'item_id']
102
        )->viaTable('order_item_with_null_fk', ['order_id' => 'id']);
103
    }
104
105
    public function getItemsInOrder1Query(): ActiveQuery
106
    {
107
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) {
108
            $q->orderBy(['subtotal' => SORT_ASC]);
109
        })->orderBy('name');
110
    }
111
112
    public function getItemsInOrder2Query(): ActiveQuery
113
    {
114
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) {
115
            $q->orderBy(['subtotal' => SORT_DESC]);
116
        })->orderBy('name');
117
    }
118
119
    public function getBooksQuery(): ActiveQuery
120
    {
121
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->where(['category_id' => 1]);
122
    }
123
124
    public function getBooksWithNullFKQuery(): ActiveQuery
125
    {
126
        return $this->hasMany(
127
            Item::class,
128
            ['id' => 'item_id']
129
        )->via('orderItemsWithNullFK')->where(['category_id' => 1]);
130
    }
131
132
    public function getBooksViaTableQuery(): ActiveQuery
133
    {
134
        return $this->hasMany(
135
            Item::class,
136
            ['id' => 'item_id']
137
        )->viaTable('order_item', ['order_id' => 'id'])->where(['category_id' => 1]);
138
    }
139
140
    public function getBooksWithNullFKViaTableQuery(): ActiveQuery
141
    {
142
        return $this->hasMany(
143
            Item::class,
144
            ['id' => 'item_id']
145
        )->viaTable('order_item_with_null_fk', ['order_id' => 'id'])->where(['category_id' => 1]);
146
    }
147
148
    public function getBooks2Query(): ActiveQuery
149
    {
150
        return $this->hasMany(
151
            Item::class,
152
            ['id' => 'item_id']
153
        )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
154
    }
155
156
    public function getBooksExplicitQuery(): ActiveQuery
157
    {
158
        return $this->hasMany(
159
            Item::class,
160
            ['id' => 'item_id']
161
        )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
162
    }
163
164
    public function getBooksExplicitAQuery(): ActiveQuery
165
    {
166
        return $this->hasMany(
167
            Item::class,
168
            ['id' => 'item_id']
169
        )->alias('bo')->onCondition(['bo.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
170
    }
171
172
    public function getBookItemsQuery(): ActiveQuery
173
    {
174
        return $this->hasMany(
175
            Item::class,
176
            ['id' => 'item_id']
177
        )->alias('books')->onCondition(['books.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
178
    }
179
180
    public function getMovieItemsQuery(): ActiveQuery
181
    {
182
        return $this->hasMany(
183
            Item::class,
184
            ['id' => 'item_id']
185
        )->alias('movies')->onCondition(['movies.category_id' => 2])->viaTable('order_item', ['order_id' => 'id']);
186
    }
187
188
    public function getLimitedItemsQuery(): ActiveQuery
189
    {
190
        return $this->hasMany(Item::class, ['id' => 'item_id'])->onCondition(['item.id' => [3, 5]])->via('orderItems');
191
    }
192
193
    public function getExpensiveItemsUsingViaWithCallableQuery(): ActiveQuery
194
    {
195
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) {
196
            $q->where(['>=', 'subtotal', 10]);
197
        });
198
    }
199
200
    public function getCheapItemsUsingViaWithCallableQuery(): ActiveQuery
201
    {
202
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) {
203
            $q->where(['<', 'subtotal', 10]);
204
        });
205
    }
206
207
    public function getOrderItemsFor8Query(): ActiveQuery
208
    {
209
        return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id'])->andOnCondition(['subtotal' => 8.0]);
210
    }
211
212
    public function getItemsFor8Query(): ActiveQuery
213
    {
214
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItemsFor8');
215
    }
216
}
217