OldClosureTableTreePathQueryBehavior.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\shareTree\behaviors;
  8. use yii\base\Behavior;
  9. use yii\db\ActiveQuery;
  10. /**
  11. *
  12. * @property ActiveQuery $owner
  13. */
  14. class OldClosureTableTreePathQueryBehavior extends Behavior
  15. {
  16. /**
  17. * 获取指定父节点下的元素
  18. *
  19. * @param int $childId
  20. * @return $this
  21. */
  22. public function byParentId(int $parentId): ActiveQuery
  23. {
  24. return $this->owner->andWhere(['parent_id' => $parentId]);
  25. }
  26. /**
  27. * 获取指定子节点上边的父元素
  28. *
  29. * @param int $childId
  30. * @return $this
  31. */
  32. public function byChildId(int $childId): ActiveQuery
  33. {
  34. return $this->owner->andWhere(['child_id' => $childId]);
  35. }
  36. /**
  37. * 通过非child_id过滤
  38. *
  39. * @param int $childId
  40. * @return $this
  41. */
  42. public function byNotChildId(int $childId): ActiveQuery
  43. {
  44. return $this->owner->andWhere(['!=', 'child_id', $childId]);
  45. }
  46. /**
  47. * 按父级别筛选
  48. *
  49. * @param int $parentLevel
  50. * @return ActiveQuery
  51. */
  52. public function byParentLevel(int $parentLevel): ActiveQuery
  53. {
  54. return $this->owner->andWhere(['parent_level' => $parentLevel]);
  55. }
  56. /**
  57. * 按子级别筛选
  58. *
  59. * @param int $childLevel
  60. * @return ActiveQuery
  61. */
  62. public function byChildLevel(int $childLevel): ActiveQuery
  63. {
  64. return $this->owner->andWhere(['child_level' => $childLevel]);
  65. }
  66. /**
  67. * 确定该节点是否为父节点的子节点
  68. *
  69. * @param int $parentId
  70. * @param int $childId
  71. * @return ActiveQuery
  72. */
  73. public function isChildOf(int $parentId, int $childId): ActiveQuery
  74. {
  75. return $this->owner
  76. ->byParentId($parentId)
  77. ->byChildId($childId)
  78. ->byNotChildId($parentId);
  79. }
  80. }