Skip to content

Commit 9d9f138

Browse files
committed
upd
1 parent c543cef commit 9d9f138

File tree

6 files changed

+40
-58
lines changed

6 files changed

+40
-58
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "smoren/yii2-query-relation-manager",
3-
"description": "Test",
3+
"description": "Class for building queries and making result array with relations (single and multiple)",
44
"type": "yii2-extension",
5-
"keywords": ["yii2","extension"],
6-
"license": "GPL-3.0+",
5+
"keywords": ["yii2","extension", "query", "builder", "relation", "ActiveRecord"],
6+
"license": "MIT",
77
"authors": [
88
{
99
"name": "Smoren",
@@ -16,7 +16,7 @@
1616
},
1717
"autoload": {
1818
"psr-4": {
19-
"smoren\\yii2_query_relation_manager\\": ""
19+
"Smoren\\Yii2\\QueryRelationManager\\": "src"
2020
}
2121
},
2222
"config": {

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exceptions/BadDataException.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

exceptions/LogicException.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

QueryRelationManager.php renamed to src/QueryRelationManager.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

33

4-
namespace smoren\yii2_query_relation_manager;
4+
namespace Smoren\Yii2\QueryRelationManager;
55

66

77
use yii\db\Query;
8-
use smoren\yii2_query_relation_manager\exceptions\BadDataException;
9-
use smoren\yii2_query_relation_manager\exceptions\LogicException;
108

119
class QueryRelationManager
1210
{
@@ -50,8 +48,7 @@ class QueryRelationManager
5048
* @param string $fieldJoinTo
5149
* @param string $primaryFieldName
5250
* @return static
53-
* @throws BadDataException
54-
* @throws LogicException
51+
* @throws QueryRelationManagerException
5552
*/
5653
public static function select(string $className, string $tableAlias, string $fieldJoinTo = 'id', string $primaryFieldName = 'id'): self
5754
{
@@ -71,8 +68,7 @@ public static function select(string $className, string $tableAlias, string $fie
7168
* @param array $extraJoinParams
7269
* @param string $primaryFieldName
7370
* @return $this
74-
* @throws BadDataException
75-
* @throws LogicException
71+
* @throws QueryRelationManagerException
7672
*/
7773
public function withSingle(
7874
string $containerFieldAlias, string $className, string $joinAs, string $joinTo,
@@ -103,8 +99,7 @@ public function withSingle(
10399
* @param array $extraJoinParams параметры для условия присоединения таблицы
104100
* @param string $primaryFieldName
105101
* @return $this
106-
* @throws BadDataException
107-
* @throws LogicException
102+
* @throws QueryRelationManagerException
108103
*/
109104
public function withMultiple(
110105
string $containerFieldAlias, string $className, string $joinAs, string $joinTo,
@@ -142,7 +137,7 @@ public function modify(string $tableAlias, callable $modifier): self
142137

143138
/**
144139
* @return array
145-
* @throws BadDataException
140+
* @throws QueryRelationManagerException
146141
*/
147142
public function all(): array
148143
{
@@ -177,7 +172,7 @@ public function all(): array
177172
$isMultiple = true;
178173
$joinTo = $this->relationMapMultiple[$joinAs];
179174
} 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...");
181176
}
182177

183178
$joinAsFieldName = $this->mapJoinAsToFieldJoinBy[$joinAs];
@@ -187,11 +182,11 @@ public function all(): array
187182

188183
foreach($itemsFrom as $id => $itemFrom) {
189184
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}");
191186
}
192187

193188
if(!isset($itemsTo[$itemFrom[$joinAsFieldName]])) {
194-
throw new BadDataException(
189+
throw new QueryRelationManagerException(
195190
"no item with {$joinAsFieldName} = {$itemFrom[$joinAsFieldName]} ".
196191
"found in items of {$joinTo}"
197192
);
@@ -202,7 +197,7 @@ public function all(): array
202197
foreach($itemsTo as &$itemTo) {
203198
if($itemTo[$joinToFieldName] == $itemFrom[$joinAsFieldName]) {
204199
if(isset($itemTo[$containerFieldName])) {
205-
throw new BadDataException(
200+
throw new QueryRelationManagerException(
206201
"trying to rewrite single relation to field {$containerFieldName} of {$joinTo}"
207202
);
208203
}
@@ -259,8 +254,7 @@ public function getRawSql(): string
259254
* @param string $alias
260255
* @param string $fieldJoinTo
261256
* @param string $primaryFieldName
262-
* @throws BadDataException
263-
* @throws LogicException
257+
* @throws QueryRelationManagerException
264258
*/
265259
protected function __construct(string $className, string $alias, string $fieldJoinTo, string $primaryFieldName = 'id')
266260
{
@@ -314,8 +308,7 @@ protected function prepare(): self
314308
* @param string|null $fieldJoinBy
315309
* @param string|null $containerFieldAlias
316310
* @return $this
317-
* @throws BadDataException
318-
* @throws LogicException
311+
* @throws QueryRelationManagerException
319312
*/
320313
protected function addAliases(
321314
string $className, string $joinAs, string $fieldJoinTo, string $primaryFieldName,
@@ -325,7 +318,7 @@ protected function addAliases(
325318
$tableName = $this->getTableName($className);
326319

327320
if(isset($this->mapJoinAsToTableName[$joinAs])) {
328-
throw new LogicException("alias {$joinAs} is already used");
321+
throw new QueryRelationManagerException("alias {$joinAs} is already used");
329322
}
330323

331324
$this->mapJoinAsToTableName[$joinAs] = $tableName;
@@ -371,19 +364,18 @@ protected function addRelationConditions(
371364
* @param string $className
372365
* @param string $joinAs
373366
* @return $this
374-
* @throws BadDataException
375-
* @throws LogicException
367+
* @throws QueryRelationManagerException
376368
*/
377369
protected function addFields(string $className, string $joinAs): self
378370
{
379371
if(!class_exists($className)) {
380-
throw new BadDataException("class {$className} is not defined");
372+
throw new QueryRelationManagerException("class {$className} is not defined");
381373
}
382374

383375
$obj = (new $className());
384376

385377
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");
387379
}
388380

389381
$fields = array_keys($obj->getAttributes());
@@ -393,7 +385,7 @@ protected function addFields(string $className, string $joinAs): self
393385
foreach($fields as $fieldName) {
394386
$fieldNameAliased = "{$joinAs}_{$fieldName}";
395387
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");
397389
}
398390

399391
$this->fieldMatrix[$joinAs][$fieldName] = $fieldNameAliased;
@@ -407,12 +399,12 @@ protected function addFields(string $className, string $joinAs): self
407399
/**
408400
* @param string $className
409401
* @return string
410-
* @throws BadDataException
402+
* @throws QueryRelationManagerException
411403
*/
412404
protected function getTableName(string $className): string
413405
{
414406
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");
416408
}
417409

418410
return $className::tableName();
@@ -423,7 +415,7 @@ protected function getTableName(string $className): string
423415
* @param array $fieldNameMap
424416
* @param string $relatedFieldName
425417
* @return array
426-
* @throws BadDataException
418+
* @throws QueryRelationManagerException
427419
*/
428420
protected function getMapFromPrefixedResult(array $result, array $fieldNameMap, string $relatedFieldName = 'id'): array
429421
{
@@ -434,14 +426,14 @@ protected function getMapFromPrefixedResult(array $result, array $fieldNameMap,
434426

435427
foreach($fieldNameMap as $fieldNamePrefixed => $fieldName) {
436428
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");
438430
}
439431

440432
$item[$fieldName] = $row[$fieldNamePrefixed];
441433
}
442434

443435
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");
445437
}
446438

447439
$map[$item[$relatedFieldName]] = $item;

src/QueryRelationManagerException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Smoren\Yii2\QueryRelationManager;
5+
6+
7+
class QueryRelationManagerException extends \Exception
8+
{
9+
10+
}

0 commit comments

Comments
 (0)