MyActiveQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\db\ActiveQuery;
  9. class MyActiveQuery extends ActiveQuery
  10. {
  11. public $myCondition = [];
  12. public function where($condition, $params = [])
  13. {
  14. if (count($this->myCondition) != 0) {
  15. $newCondition = $this->unsetArray($this->myCondition, $condition);
  16. if (count($newCondition) != 0) {
  17. parent::where($newCondition);
  18. return parent::andWhere($condition, $params);
  19. } else {
  20. return parent::where($condition, $params);
  21. }
  22. } else {
  23. return parent::where($condition, $params);
  24. }
  25. }
  26. public function andWhere($condition, $params = [])
  27. {
  28. if($this->where === null){
  29. if (count($this->myCondition) != 0) {
  30. $newCondition = $this->unsetArray($this->myCondition, $condition);
  31. parent::andWhere($newCondition);
  32. }
  33. }else{
  34. $where = $this->where;
  35. if(isset($where[1])){
  36. $arr = $where[1];
  37. }else{
  38. $arr = $where;
  39. }
  40. $newCondition = $this->unsetArray($arr, $condition);
  41. parent::where($newCondition);
  42. foreach($where as $key=>$value){
  43. if($key <= 1){
  44. continue;
  45. }
  46. parent::andWhere($value);
  47. }
  48. }
  49. return parent::andWhere($condition, $params); // TODO: Change the autogenerated stub
  50. }
  51. /**
  52. * @param $arr
  53. * @param $condition // 四种情况 1、'is_show = 1' 字符串 2、['is_show'=>1] 键值对数组 3、['!=','is_show',1]数组 4、['or',['is_show'=>1],'is_show = 0',['!=','is_show',1]];
  54. * @return array
  55. */
  56. private function unsetArray($arr, $condition)
  57. {
  58. $newCondition = [];
  59. foreach ($arr as $key => $item) {
  60. if (is_string($condition)) {
  61. if ($this->newStrstr($condition, $key) == false) {
  62. $newCondition[$key] = $item;
  63. }
  64. } else if (is_array($condition)) {
  65. $arr = [$key, $item];
  66. $ok = $this->conditionArray($arr, $condition);
  67. if ($ok) {
  68. $newCondition[$key] = $item;
  69. }
  70. } else {
  71. $newCondition[$key] = $item;
  72. }
  73. }
  74. return $newCondition;
  75. }
  76. private function conditionArray($arr, $condition)
  77. {
  78. $ok = true;
  79. $type = 0;
  80. foreach ($condition as $k => $v) {
  81. if (is_numeric($k)) {
  82. if ($k == 0) {
  83. if (in_array($v, ['or', 'and', 'Or', 'OR', 'AND', 'And'])) {
  84. $type = 0;
  85. } else {
  86. $type = 1;
  87. }
  88. }
  89. } else {
  90. $type = 2;
  91. if ($this->newStrstr($k, $arr[0])) {
  92. $ok = false;
  93. break;
  94. }
  95. }
  96. if ($type == 0) {
  97. if ($k == 0) {
  98. continue;
  99. }
  100. if (is_array($v)) {
  101. $okA = $this->conditionArray($arr, $v);
  102. if ($okA == false) {
  103. $ok = false;
  104. break;
  105. }
  106. } else {
  107. if ($k == 1 && $this->newStrstr($v, $arr[0])) {
  108. $ok = false;
  109. break;
  110. }
  111. }
  112. } elseif ($type == 1) {
  113. if (isset($condition[1]) && is_string($condition[1])) {
  114. if ($this->newStrstr($condition[1], $arr[0])) {
  115. $ok = false;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. return $ok;
  122. }
  123. // 比较
  124. private function newStrstr($a, $b)
  125. {
  126. return strstr($a, $b);
  127. }
  128. }