DictionaryRelationshipService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace common\services;
  3. use common\models\DictionaryRelationship;
  4. class DictionaryRelationshipService
  5. {
  6. public static function getDictionaryDeriveInfo(): array
  7. {
  8. $data = [];
  9. $DictionaryRelationshipList = DictionaryRelationship::find()->alias('r')
  10. ->join('LEFT JOIN', 'dictionary d', 'd.id = r.relation_id')
  11. ->select("r.*,d.param_name")->asArray()->all();
  12. foreach ($DictionaryRelationshipList as $DictionaryRelationship){
  13. $data[$DictionaryRelationship["dictionary_id"]][] = $DictionaryRelationship;
  14. }
  15. return $data;
  16. }
  17. public static function addByDerive($deriveList, $dictionary_id): void
  18. {
  19. foreach ($deriveList as $derive) {
  20. $relationship = DictionaryRelationship::findOne(["dictionary_id" => $dictionary_id, "relation_id" => $derive["deriver_id"]]);
  21. if (!$relationship) {
  22. $relationship = new DictionaryRelationship();
  23. $relationship->dictionary_id = $dictionary_id;
  24. $relationship->relation_id = $derive["deriver_id"];
  25. $relationship->save();
  26. }
  27. }
  28. }
  29. public static function updateByDerive($deriveList, $dictionary_id): void
  30. {
  31. $relationshipList = DictionaryRelationship::find()->where(["dictionary_id" => $dictionary_id])->all();
  32. if ($relationshipList) {
  33. foreach ($relationshipList as $key => $relationship) {
  34. /** @var DictionaryRelationship $relationship */
  35. $relationship->relation_id = $deriveList[$key]["deriver_id"];
  36. $relationship->save();
  37. }
  38. } else {
  39. self::addByDerive($deriveList, $dictionary_id);
  40. }
  41. }
  42. }