Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 29 |
| RelationHelper | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
90.00 | |
0.00% |
0 / 29 |
| GetJunctionListName | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| GetJunctionList | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
| SetJunctionList | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
| UpdateJunctionList | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 18 |
|||
| <?php | |
| namespace common\components; | |
| use Yii; | |
| class RelationHelper | |
| { | |
| protected static function GetJunctionListName($relationName) | |
| { | |
| return $relationName . 'List'; | |
| } | |
| protected static function GetJunctionList($activeRecord, $relationName, $propertyName = 'id') | |
| { | |
| $relation = $activeRecord->getRelation($relationName); | |
| return $relation->select($propertyName)->column(); | |
| } | |
| public static function SetJunctionList($activeRecord, $relationName, $propertyName = 'id') | |
| { | |
| $listPropertyName = self::GetJunctionListName($relationName); | |
| $activeRecord->$listPropertyName = self::GetJunctionList($activeRecord, $relationName, $propertyName); | |
| } | |
| public static function UpdateJunctionList($activeRecord, $relationName, $propertyName = 'id') | |
| { | |
| $listPropertyName = self::GetJunctionListName($relationName); | |
| $storedList = self::GetJunctionList($activeRecord, $relationName); | |
| $relation = $activeRecord->getRelation($relationName); | |
| $foreignModel = Yii::createObject($relation->modelClass); | |
| if(empty($activeRecord->$listPropertyName)) | |
| $activeRecord->$listPropertyName = []; | |
| $toUpdateList = array_diff($activeRecord->$listPropertyName, $storedList); | |
| foreach($toUpdateList as $id){ | |
| $foreignActiveRecord = $foreignModel->findOne($id); | |
| if(!empty($foreignActiveRecord)) $activeRecord->link($relationName, $foreignActiveRecord); | |
| } | |
| $toDeleteList = array_diff($storedList, $activeRecord->$listPropertyName); | |
| foreach($toDeleteList as $id){ | |
| $foreignActiveRecord = $foreignModel->findOne($id); | |
| if(!empty($foreignActiveRecord)) $activeRecord->unlink($relationName, $foreignActiveRecord, !empty($relation->via)); | |
| } | |
| } | |
| } |