Passed
Pull Request — master (#83)
by Wilmer
24:07 queued 09:08
created

Order::getBooksWithNullFK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 static ?string $tableName = null;
21
22
    public static function tableName(): string
23
    {
24
        return static::$tableName ?: 'order';
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'])
35
            ->joinWith('profile');
36
    }
37
38
    public function getCustomerJoinedWithProfileIndexOrdered(): ActiveQuery
39
    {
40
        return $this->hasMany(Customer::class, ['id' => 'customer_id'])
41
            ->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name');
42
    }
43
44
    public function getCustomer2(): ActiveQuery
45
    {
46
        return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders2');
47
    }
48
49
    public function getOrderItems(): ActiveQuery
50
    {
51
        return $this->hasMany(OrderItem::class, ['order_id' => 'id']);
52
    }
53
54
    public function getOrderItems2(): ActiveQuery
55
    {
56
        return $this->hasMany(OrderItem::class, ['order_id' => 'id'])
57
            ->indexBy('item_id');
58
    }
59
60
    public function getOrderItems3(): ActiveQuery
61
    {
62
        return $this->hasMany(OrderItem::class, ['order_id' => 'id'])
63
            ->indexBy(function ($row) {
64
                return $row['order_id'] . '_' . $row['item_id'];
65
            });
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'])
76
            ->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

76
            ->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...
77
                // additional query configuration
78
            })->orderBy('item.id');
79
    }
80
81
    public function getItemsIndexed(): ActiveQuery
82
    {
83
        return $this->hasMany(Item::class, ['id' => 'item_id'])
84
            ->via('orderItems')->indexBy('id');
85
    }
86
87
    public function getItemsWithNullFK(): ActiveQuery
88
    {
89
        return $this->hasMany(Item::class, ['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'])
96
            ->via('orderItems', static function ($q) {
97
                $q->orderBy(['subtotal' => SORT_ASC]);
98
            })->orderBy('name');
99
    }
100
101
    public function getItemsInOrder2(): ActiveQuery
102
    {
103
        return $this->hasMany(Item::class, ['id' => 'item_id'])
104
            ->via('orderItems', static function ($q) {
105
                $q->orderBy(['subtotal' => SORT_DESC]);
106
            })->orderBy('name');
107
    }
108
109
    public function getBooks(): ActiveQuery
110
    {
111
        return $this->hasMany(Item::class, ['id' => 'item_id'])
112
            ->via('orderItems')
113
            ->where(['category_id' => 1]);
114
    }
115
116
    public function getBooksWithNullFK(): ActiveQuery
117
    {
118
        return $this->hasMany(Item::class, ['id' => 'item_id'])
119
            ->via('orderItemsWithNullFK')
120
            ->where(['category_id' => 1]);
121
    }
122
123
    public function getBooksViaTable(): ActiveQuery
124
    {
125
        return $this->hasMany(Item::class, ['id' => 'item_id'])
126
            ->viaTable('order_item', ['order_id' => 'id'])
127
            ->where(['category_id' => 1]);
128
    }
129
130
    public function getBooksWithNullFKViaTable(): ActiveQuery
131
    {
132
        return $this->hasMany(Item::class, ['id' => 'item_id'])
133
            ->viaTable('order_item_with_null_fk', ['order_id' => 'id'])
134
            ->where(['category_id' => 1]);
135
    }
136
137
    public function getBooks2(): ActiveQuery
138
    {
139
        return $this->hasMany(Item::class, ['id' => 'item_id'])
140
            ->onCondition(['category_id' => 1])
141
            ->viaTable('order_item', ['order_id' => 'id']);
142
    }
143
144
    public function getBooksExplicit(): ActiveQuery
145
    {
146
        return $this->hasMany(Item::class, ['id' => 'item_id'])
147
            ->onCondition(['category_id' => 1])
148
            ->viaTable('order_item', ['order_id' => 'id']);
149
    }
150
151
    public function getBooksExplicitA(): ActiveQuery
152
    {
153
        return $this->hasMany(Item::class, ['id' => 'item_id'])->alias('bo')
154
            ->onCondition(['bo.category_id' => 1])
155
            ->viaTable('order_item', ['order_id' => 'id']);
156
    }
157
158
    public function getBookItems(): ActiveQuery
159
    {
160
        return $this->hasMany(Item::class, ['id' => 'item_id'])->alias('books')
161
            ->onCondition(['books.category_id' => 1])
162
            ->viaTable('order_item', ['order_id' => 'id']);
163
    }
164
165
    public function getMovieItems(): ActiveQuery
166
    {
167
        return $this->hasMany(Item::class, ['id' => 'item_id'])->alias('movies')
168
            ->onCondition(['movies.category_id' => 2])
169
            ->viaTable('order_item', ['order_id' => 'id']);
170
    }
171
172
    public function getLimitedItems(): ActiveQuery
173
    {
174
        return $this->hasMany(Item::class, ['id' => 'item_id'])
175
            ->onCondition(['item.id' => [3, 5]])
176
            ->via('orderItems');
177
    }
178
179
    public function getExpensiveItemsUsingViaWithCallable()
180
    {
181
        return $this->hasMany(Item::class, ['id' => 'item_id'])
182
            ->via('orderItems', function (ActiveQuery $q) {
183
                $q->where(['>=', 'subtotal', 10]);
184
            });
185
    }
186
187
    public function getCheapItemsUsingViaWithCallable()
188
    {
189
        return $this->hasMany(Item::class, ['id' => 'item_id'])
190
            ->via('orderItems', function (ActiveQuery $q) {
191
                $q->where(['<', 'subtotal', 10]);
192
            });
193
    }
194
195
    public function beforeSave($insert): bool
196
    {
197
        if (parent::beforeSave($insert)) {
0 ignored issues
show
Bug introduced by
The method beforeSave() does not exist on Yiisoft\ActiveRecord\ActiveRecord. It seems like you code against a sub-type of Yiisoft\ActiveRecord\ActiveRecord such as Yiisoft\ActiveRecord\Tes...tubs\ActiveRecord\Order. ( Ignorable by Annotation )

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

197
        if (parent::/** @scrutinizer ignore-call */ beforeSave($insert)) {
Loading history...
198
            $this->created_at = time();
199
200
            return true;
201
        }
202
203
        return false;
204
    }
205
206
    public function attributeLabels(): array
207
    {
208
        return [
209
            'customer_id' => 'Customer',
210
            'total' => 'Invoice Total',
211
        ];
212
    }
213
214
    public function activeAttributes(): array
215
    {
216
        return [
217
            0 => 'customer_id',
218
        ];
219
    }
220
}
221