1
1
<?php
2
2
3
3
4
- namespace smoren \ yii2_query_relation_manager ;
4
+ namespace Smoren \ Yii2 \ QueryRelationManager ;
5
5
6
6
7
7
use yii \db \Query ;
8
- use smoren \yii2_query_relation_manager \exceptions \BadDataException ;
9
- use smoren \yii2_query_relation_manager \exceptions \LogicException ;
10
8
11
9
class QueryRelationManager
12
10
{
@@ -50,8 +48,7 @@ class QueryRelationManager
50
48
* @param string $fieldJoinTo
51
49
* @param string $primaryFieldName
52
50
* @return static
53
- * @throws BadDataException
54
- * @throws LogicException
51
+ * @throws QueryRelationManagerException
55
52
*/
56
53
public static function select (string $ className , string $ tableAlias , string $ fieldJoinTo = 'id ' , string $ primaryFieldName = 'id ' ): self
57
54
{
@@ -71,8 +68,7 @@ public static function select(string $className, string $tableAlias, string $fie
71
68
* @param array $extraJoinParams
72
69
* @param string $primaryFieldName
73
70
* @return $this
74
- * @throws BadDataException
75
- * @throws LogicException
71
+ * @throws QueryRelationManagerException
76
72
*/
77
73
public function withSingle (
78
74
string $ containerFieldAlias , string $ className , string $ joinAs , string $ joinTo ,
@@ -103,8 +99,7 @@ public function withSingle(
103
99
* @param array $extraJoinParams параметры для условия присоединения таблицы
104
100
* @param string $primaryFieldName
105
101
* @return $this
106
- * @throws BadDataException
107
- * @throws LogicException
102
+ * @throws QueryRelationManagerException
108
103
*/
109
104
public function withMultiple (
110
105
string $ containerFieldAlias , string $ className , string $ joinAs , string $ joinTo ,
@@ -142,7 +137,7 @@ public function modify(string $tableAlias, callable $modifier): self
142
137
143
138
/**
144
139
* @return array
145
- * @throws BadDataException
140
+ * @throws QueryRelationManagerException
146
141
*/
147
142
public function all (): array
148
143
{
@@ -177,7 +172,7 @@ public function all(): array
177
172
$ isMultiple = true ;
178
173
$ joinTo = $ this ->relationMapMultiple [$ joinAs ];
179
174
} else {
180
- throw new LogicException ("something went wrong and we don't care... " );
175
+ throw new QueryRelationManagerException ("something went wrong and we don't care... " );
181
176
}
182
177
183
178
$ joinAsFieldName = $ this ->mapJoinAsToFieldJoinBy [$ joinAs ];
@@ -187,11 +182,11 @@ public function all(): array
187
182
188
183
foreach ($ itemsFrom as $ id => $ itemFrom ) {
189
184
if (!isset ($ itemFrom [$ joinAsFieldName ])) {
190
- throw new BadDataException ("no field {$ joinAsFieldName } found in items of {$ joinAs }" );
185
+ throw new QueryRelationManagerException ("no field {$ joinAsFieldName } found in items of {$ joinAs }" );
191
186
}
192
187
193
188
if (!isset ($ itemsTo [$ itemFrom [$ joinAsFieldName ]])) {
194
- throw new BadDataException (
189
+ throw new QueryRelationManagerException (
195
190
"no item with {$ joinAsFieldName } = {$ itemFrom [$ joinAsFieldName ]} " .
196
191
"found in items of {$ joinTo }"
197
192
);
@@ -202,7 +197,7 @@ public function all(): array
202
197
foreach ($ itemsTo as &$ itemTo ) {
203
198
if ($ itemTo [$ joinToFieldName ] == $ itemFrom [$ joinAsFieldName ]) {
204
199
if (isset ($ itemTo [$ containerFieldName ])) {
205
- throw new BadDataException (
200
+ throw new QueryRelationManagerException (
206
201
"trying to rewrite single relation to field {$ containerFieldName } of {$ joinTo }"
207
202
);
208
203
}
@@ -259,8 +254,7 @@ public function getRawSql(): string
259
254
* @param string $alias
260
255
* @param string $fieldJoinTo
261
256
* @param string $primaryFieldName
262
- * @throws BadDataException
263
- * @throws LogicException
257
+ * @throws QueryRelationManagerException
264
258
*/
265
259
protected function __construct (string $ className , string $ alias , string $ fieldJoinTo , string $ primaryFieldName = 'id ' )
266
260
{
@@ -314,8 +308,7 @@ protected function prepare(): self
314
308
* @param string|null $fieldJoinBy
315
309
* @param string|null $containerFieldAlias
316
310
* @return $this
317
- * @throws BadDataException
318
- * @throws LogicException
311
+ * @throws QueryRelationManagerException
319
312
*/
320
313
protected function addAliases (
321
314
string $ className , string $ joinAs , string $ fieldJoinTo , string $ primaryFieldName ,
@@ -325,7 +318,7 @@ protected function addAliases(
325
318
$ tableName = $ this ->getTableName ($ className );
326
319
327
320
if (isset ($ this ->mapJoinAsToTableName [$ joinAs ])) {
328
- throw new LogicException ("alias {$ joinAs } is already used " );
321
+ throw new QueryRelationManagerException ("alias {$ joinAs } is already used " );
329
322
}
330
323
331
324
$ this ->mapJoinAsToTableName [$ joinAs ] = $ tableName ;
@@ -371,19 +364,18 @@ protected function addRelationConditions(
371
364
* @param string $className
372
365
* @param string $joinAs
373
366
* @return $this
374
- * @throws BadDataException
375
- * @throws LogicException
367
+ * @throws QueryRelationManagerException
376
368
*/
377
369
protected function addFields (string $ className , string $ joinAs ): self
378
370
{
379
371
if (!class_exists ($ className )) {
380
- throw new BadDataException ("class {$ className } is not defined " );
372
+ throw new QueryRelationManagerException ("class {$ className } is not defined " );
381
373
}
382
374
383
375
$ obj = (new $ className ());
384
376
385
377
if (!method_exists ($ obj , 'getAttributes ' )) {
386
- throw new BadDataException ("method {$ className }::getAttributes() is not defined " );
378
+ throw new QueryRelationManagerException ("method {$ className }::getAttributes() is not defined " );
387
379
}
388
380
389
381
$ fields = array_keys ($ obj ->getAttributes ());
@@ -393,7 +385,7 @@ protected function addFields(string $className, string $joinAs): self
393
385
foreach ($ fields as $ fieldName ) {
394
386
$ fieldNameAliased = "{$ joinAs }_ {$ fieldName }" ;
395
387
if (isset ($ this ->fieldMap [$ fieldNameAliased ])) {
396
- throw new LogicException ("aliased field name {$ fieldNameAliased } already used " );
388
+ throw new QueryRelationManagerException ("aliased field name {$ fieldNameAliased } already used " );
397
389
}
398
390
399
391
$ this ->fieldMatrix [$ joinAs ][$ fieldName ] = $ fieldNameAliased ;
@@ -407,12 +399,12 @@ protected function addFields(string $className, string $joinAs): self
407
399
/**
408
400
* @param string $className
409
401
* @return string
410
- * @throws BadDataException
402
+ * @throws QueryRelationManagerException
411
403
*/
412
404
protected function getTableName (string $ className ): string
413
405
{
414
406
if (!method_exists ($ className , 'tableName ' )) {
415
- throw new BadDataException ("method {$ className }::tableName() is not defined " );
407
+ throw new QueryRelationManagerException ("method {$ className }::tableName() is not defined " );
416
408
}
417
409
418
410
return $ className ::tableName ();
@@ -423,7 +415,7 @@ protected function getTableName(string $className): string
423
415
* @param array $fieldNameMap
424
416
* @param string $relatedFieldName
425
417
* @return array
426
- * @throws BadDataException
418
+ * @throws QueryRelationManagerException
427
419
*/
428
420
protected function getMapFromPrefixedResult (array $ result , array $ fieldNameMap , string $ relatedFieldName = 'id ' ): array
429
421
{
@@ -434,14 +426,14 @@ protected function getMapFromPrefixedResult(array $result, array $fieldNameMap,
434
426
435
427
foreach ($ fieldNameMap as $ fieldNamePrefixed => $ fieldName ) {
436
428
if (!array_key_exists ($ fieldNamePrefixed , $ row )) {
437
- throw new BadDataException ("no field {$ fieldNamePrefixed } in result row " );
429
+ throw new QueryRelationManagerException ("no field {$ fieldNamePrefixed } in result row " );
438
430
}
439
431
440
432
$ item [$ fieldName ] = $ row [$ fieldNamePrefixed ];
441
433
}
442
434
443
435
if (!isset ($ item [$ relatedFieldName ])) {
444
- throw new BadDataException ("no field {$ relatedFieldName } in result row for mapping " );
436
+ throw new QueryRelationManagerException ("no field {$ relatedFieldName } in result row for mapping " );
445
437
}
446
438
447
439
$ map [$ item [$ relatedFieldName ]] = $ item ;
0 commit comments