Passed
Pull Request — master (#203)
by Wilmer
02:45
created

Order::setTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 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 static function tableName(): string
21
    {
22
        return 'order';
23
    }
24
25
    public function getCustomer(): ActiveQuery
26
    {
27
        return $this->hasOne(Customer::class, ['id' => 'customer_id']);
28
    }
29
30
    public function getCustomerJoinedWithProfile(): ActiveQuery
31
    {
32
        return $this->hasOne(Customer::class, ['id' => 'customer_id'])->joinWith('profile');
33
    }
34
35
    public function getCustomerJoinedWithProfileIndexOrdered(): ActiveQuery
36
    {
37
        return $this->hasMany(
38
            Customer::class,
39
            ['id' => 'customer_id']
40
        )->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name');
41
    }
42
43
    public function getCustomer2(): ActiveQuery
44
    {
45
        return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders2');
46
    }
47
48
    public function getOrderItems(): ActiveQuery
49
    {
50
        return $this->hasMany(OrderItem::class, ['order_id' => 'id']);
51
    }
52
53
    public function getOrderItems2(): ActiveQuery
54
    {
55
        return $this->hasMany(OrderItem::class, ['order_id' => 'id'])->indexBy('item_id');
56
    }
57
58
    public function getOrderItems3(): ActiveQuery
59
    {
60
        return $this->hasMany(
61
            OrderItem::class,
62
            ['order_id' => 'id']
63
        )->indexBy(fn ($row) => $row['order_id'] . '_' . $row['item_id']);
64
    }
65
66
    public function getOrderItemsWithNullFK(): ActiveQuery
67
    {
68
        return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id']);
69
    }
70
71
    public function getItems(): ActiveQuery
72
    {
73
        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

73
        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...
74
            // additional query configuration
75
        })->orderBy('item.id');
76
    }
77
78
    public function getItemsIndexed(): ActiveQuery
79
    {
80
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->indexBy('id');
81
    }
82
83
    public function getItemsWithNullFK(): ActiveQuery
84
    {
85
        return $this->hasMany(
86
            Item::class,
87
            ['id' => 'item_id']
88
        )->viaTable('order_item_with_null_fk', ['order_id' => 'id']);
89
    }
90
91
    public function getItemsInOrder1(): ActiveQuery
92
    {
93
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) {
94
            $q->orderBy(['subtotal' => SORT_ASC]);
95
        })->orderBy('name');
96
    }
97
98
    public function getItemsInOrder2(): ActiveQuery
99
    {
100
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) {
101
            $q->orderBy(['subtotal' => SORT_DESC]);
102
        })->orderBy('name');
103
    }
104
105
    public function getBooks(): ActiveQuery
106
    {
107
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->where(['category_id' => 1]);
108
    }
109
110
    public function getBooksWithNullFK(): ActiveQuery
111
    {
112
        return $this->hasMany(
113
            Item::class,
114
            ['id' => 'item_id']
115
        )->via('orderItemsWithNullFK')->where(['category_id' => 1]);
116
    }
117
118
    public function getBooksViaTable(): ActiveQuery
119
    {
120
        return $this->hasMany(
121
            Item::class,
122
            ['id' => 'item_id']
123
        )->viaTable('order_item', ['order_id' => 'id'])->where(['category_id' => 1]);
124
    }
125
126
    public function getBooksWithNullFKViaTable(): ActiveQuery
127
    {
128
        return $this->hasMany(
129
            Item::class,
130
            ['id' => 'item_id']
131
        )->viaTable('order_item_with_null_fk', ['order_id' => 'id'])->where(['category_id' => 1]);
132
    }
133
134
    public function getBooks2(): ActiveQuery
135
    {
136
        return $this->hasMany(
137
            Item::class,
138
            ['id' => 'item_id']
139
        )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
140
    }
141
142
    public function getBooksExplicit(): ActiveQuery
143
    {
144
        return $this->hasMany(
145
            Item::class,
146
            ['id' => 'item_id']
147
        )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
148
    }
149
150
    public function getBooksExplicitA(): ActiveQuery
151
    {
152
        return $this->hasMany(
153
            Item::class,
154
            ['id' => 'item_id']
155
        )->alias('bo')->onCondition(['bo.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
156
    }
157
158
    public function getBookItems(): ActiveQuery
159
    {
160
        return $this->hasMany(
161
            Item::class,
162
            ['id' => 'item_id']
163
        )->alias('books')->onCondition(['books.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']);
164
    }
165
166
    public function getMovieItems(): ActiveQuery
167
    {
168
        return $this->hasMany(
169
            Item::class,
170
            ['id' => 'item_id']
171
        )->alias('movies')->onCondition(['movies.category_id' => 2])->viaTable('order_item', ['order_id' => 'id']);
172
    }
173
174
    public function getLimitedItems(): ActiveQuery
175
    {
176
        return $this->hasMany(Item::class, ['id' => 'item_id'])->onCondition(['item.id' => [3, 5]])->via('orderItems');
177
    }
178
179
    public function getExpensiveItemsUsingViaWithCallable(): ActiveQuery
180
    {
181
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) {
182
            $q->where(['>=', 'subtotal', 10]);
183
        });
184
    }
185
186
    public function getCheapItemsUsingViaWithCallable(): ActiveQuery
187
    {
188
        return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) {
189
            $q->where(['<', 'subtotal', 10]);
190
        });
191
    }
192
193
    public function activeAttributes(): array
194
    {
195
        return [
196
            0 => 'customer_id',
197
        ];
198
    }
199
}
200