7
7
use Yiisoft \ActiveRecord \ActiveQueryInterface ;
8
8
use Yiisoft \ActiveRecord \ActiveRecordInterface ;
9
9
use Yiisoft \Db \Expression \ExpressionInterface ;
10
+ use Yiisoft \ActiveRecord \NotFoundException ;
10
11
11
12
/**
12
13
* Trait to support static methods {@see find()}, {@see findOne()}, {@see findAll()}, {@see findBySql()} to find records.
@@ -43,6 +44,10 @@ trait RepositoryTrait
43
44
* Returns an instance of {@see ActiveQueryInterface} instantiated by {@see ActiveRecordInterface::query()} method.
44
45
* If the `$condition` parameter is not null, it calls {@see ActiveQueryInterface::andWhere()} method.
45
46
* Do not to pass user input to this method, use {@see findByPk()} instead.
47
+ *
48
+ * @param array|ExpressionInterface|string|null $condition The condition to be applied to the query where clause.
49
+ * No condition is applied if `null` (by default).
50
+ * @param array $params The parameters to be bound to the SQL statement during execution.
46
51
*/
47
52
public static function find (array |string |ExpressionInterface |null $ condition = null , array $ params = []): ActiveQueryInterface
48
53
{
@@ -87,13 +92,38 @@ public static function find(array|string|ExpressionInterface|null $condition = n
87
92
* $post = Post::findByPk($id);
88
93
* ```
89
94
*
95
+ * @param array|ExpressionInterface|string|null $condition The condition to be applied to the query where clause.
96
+ * Returns all records if `null` (by default).
97
+ * @param array $params The parameters to be bound to the SQL statement during execution.
98
+ *
90
99
* @return ActiveRecordInterface[]|array[] An array of ActiveRecord instance, or an empty array if nothing matches.
91
100
*/
92
101
public static function findAll (array |string |ExpressionInterface |null $ condition = null , array $ params = []): array
93
102
{
94
103
return static ::find ($ condition , $ params )->all ();
95
104
}
96
105
106
+ /**
107
+ * Shortcut for {@see findAll()} method with throwing {@see NotFoundException} if no records found.
108
+ *
109
+ * ```php
110
+ * $customers = Customer::findAllOrFail(['is_active' => true]);
111
+ * ```
112
+ *
113
+ * @param array|ExpressionInterface|string|null $condition The condition to be applied to the query where clause.
114
+ * Returns all records if `null` (by default).
115
+ * @param array $params The parameters to be bound to the SQL statement during execution.
116
+ *
117
+ * @throws NotFoundException
118
+ *
119
+ * @return ActiveRecordInterface[]|array[] An array of ActiveRecord instance, or throws {@see NotFoundException}
120
+ * if nothing matches.
121
+ */
122
+ public static function findAllOrFail (array |string |ExpressionInterface |null $ condition = null , array $ params = []): array
123
+ {
124
+ return static ::findAll ($ condition , $ params ) ?: throw new NotFoundException ('No records found. ' );
125
+ }
126
+
97
127
/**
98
128
* Finds an ActiveRecord instance by the given primary key value.
99
129
* In the examples below, the `id` column is the primary key of the table.
@@ -123,17 +153,40 @@ public static function findAll(array|string|ExpressionInterface|null $condition
123
153
* $customer = Customer::findByPk($id);
124
154
* }
125
155
* ```
156
+ *
157
+ * @param array|float|int|string $values The primary key value(s) to find the record.
158
+ *
159
+ * @return ActiveRecordInterface|array|null Instance matching the primary key value(s), or `null` if nothing matches.
126
160
*/
127
161
public static function findByPk (array |float |int |string $ values ): array |ActiveRecordInterface |null
128
162
{
129
163
return static ::instantiate ()->query ()->findByPk ($ values );
130
164
}
131
165
132
166
/**
133
- * Creates an {@see ActiveQuery} instance with a given SQL statement.
167
+ * Shortcut for {@see findByPk()} method with throwing {@see NotFoundException} if no records found.
168
+ *
169
+ * ```php
170
+ * $customer = Customer::findByPkOrFail(1);
171
+ * ```
172
+ *
173
+ * @param array|float|int|string $values The primary key value(s) to find the record.
174
+ *
175
+ * @throws NotFoundException
176
+ *
177
+ * @return ActiveRecordInterface|array|null Instance matching the primary key value(s),
178
+ * or throws {@see NotFoundException} if nothing matches.
179
+ */
180
+ public static function findByPkOrFail (array |float |int |string $ values ): array |ActiveRecordInterface |null
181
+ {
182
+ return static ::findByPk ($ values ) ?? throw new NotFoundException ('No records found. ' );
183
+ }
184
+
185
+ /**
186
+ * Creates an {@see ActiveQueryInterface} instance with a given SQL statement.
134
187
*
135
188
* Note: That because the SQL statement is already specified, calling more query modification methods
136
- * (such as {@see where()}, {@see order()) on the created {@see ActiveQuery } instance will have no effect.
189
+ * (such as {@see where()}, {@see order()) on the created {@see ActiveQueryInterface } instance will have no effect.
137
190
*
138
191
* However, calling {@see with()}, {@see asArray()}, {@see indexBy()} or {@see resultCallback()} is still fine.
139
192
*
@@ -145,6 +198,8 @@ public static function findByPk(array|float|int|string $values): array|ActiveRec
145
198
*
146
199
* @param string $sql The SQL statement to be executed.
147
200
* @param array $params The parameters to be bound to the SQL statement during execution.
201
+ *
202
+ * @return ActiveQueryInterface The newly created {@see ActiveQueryInterface} instance.
148
203
*/
149
204
public static function findBySql (string $ sql , array $ params = []): ActiveQueryInterface
150
205
{
@@ -183,6 +238,10 @@ public static function findBySql(string $sql, array $params = []): ActiveQueryIn
183
238
* $post = Post::findByPk($id);
184
239
* ```
185
240
*
241
+ * @param array|ExpressionInterface|string|null $condition The condition to be applied to the query where clause.
242
+ * Returns the first record if `null` (by default).
243
+ * @param array $params The parameters to be bound to the SQL statement during execution.
244
+ *
186
245
* @return ActiveRecordInterface|array|null Instance matching the condition, or `null` if nothing matches.
187
246
*/
188
247
public static function findOne (
@@ -192,6 +251,29 @@ public static function findOne(
192
251
return static ::find ($ condition , $ params )->one ();
193
252
}
194
253
254
+ /**
255
+ * Shortcut for {@see findOne()} method with throwing {@see NotFoundException} if no records found.
256
+ *
257
+ * ```php
258
+ * $customer = Customer::findOneOrFail(['id' => 1]);
259
+ * ```
260
+ *
261
+ * @param array|ExpressionInterface|string|null $condition The condition to be applied to the query where clause.
262
+ * Returns the first record if `null` (by default).
263
+ * @param array $params The parameters to be bound to the SQL statement during execution.
264
+ *
265
+ * @throws NotFoundException
266
+ *
267
+ * @return ActiveRecordInterface|array|null Instance matching the condition, or throws {@see NotFoundException}
268
+ * if nothing matches.
269
+ */
270
+ public static function findOneOrFail (
271
+ array |string |ExpressionInterface |null $ condition = null ,
272
+ array $ params = [],
273
+ ): ActiveRecordInterface |array |null {
274
+ return static ::findOne ($ condition , $ params ) ?? throw new NotFoundException ('No records found. ' );
275
+ }
276
+
195
277
protected static function instantiate (): ActiveRecordInterface
196
278
{
197
279
return new static ();
0 commit comments