SearchModel.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace common\models\base;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\data\Pagination;
  6. use yii\db\ActiveQuery;
  7. use yii\validators\Validator;
  8. use yii\web\NotFoundHttpException;
  9. /**
  10. * // 示例一
  11. *
  12. * ```php
  13. * $searchModel = new SearchModel(
  14. * [
  15. * 'model' => Topic::class,
  16. * 'scenario' => 'default',
  17. * ]
  18. * );
  19. *
  20. * $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  21. *
  22. * return $this->render('index', [
  23. * 'dataProvider' => $dataProvider,
  24. * ]);
  25. * ```
  26. *
  27. * // 示例二
  28. *
  29. *```php
  30. * $searchModel = new SearchModel(
  31. * [
  32. * 'defaultOrder' => ['id' => SORT_DESC],
  33. * 'model' => Topic::class,
  34. * 'scenario' => 'default',
  35. * 'relations' => ['comment' => []], // 关联表(可以是Model里面的关联)
  36. * 'partialMatchAttributes' => ['title'], // 模糊查询
  37. * 'pageSize' => 15
  38. * ]
  39. * );
  40. *
  41. * $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  42. * $dataProvider->query->andWhere([Topic::tableName() . '.user_id' => 23, Comment::tableName() . '.status' => 1]);
  43. *
  44. * return $this->render('index', [
  45. * 'dataProvider' => $dataProvider,
  46. * ]);
  47. * ```
  48. *
  49. * Class SearchModel
  50. * @package common\components
  51. * @property \yii\db\ActiveRecord|\yii\base\Model $model
  52. */
  53. class SearchModel extends Model
  54. {
  55. private $attributes;
  56. private $attributeLabels;
  57. private $internalRelations;
  58. private $model;
  59. private $modelClassName;
  60. private $relationAttributes = [];
  61. private $rules;
  62. private $scenarios;
  63. /**
  64. * @var string 默认排序
  65. */
  66. public $defaultOrder;
  67. /**
  68. * @var string 分组
  69. */
  70. public $groupBy;
  71. /**
  72. * @var int 每页大小
  73. */
  74. public $pageSize = 10;
  75. /**
  76. * @var array 模糊查询
  77. */
  78. public $partialMatchAttributes = [];
  79. /**
  80. * @var array
  81. */
  82. public $relations = [];
  83. /**
  84. * SearchModel constructor.
  85. * @param $params
  86. * @throws NotFoundHttpException
  87. */
  88. public function __construct($params)
  89. {
  90. $this->scenario = 'search';
  91. parent::__construct($params);
  92. if ($this->model === null) {
  93. throw new NotFoundHttpException('Param "model" cannot be empty');
  94. }
  95. $this->rules = $this->model->rules();
  96. $this->scenarios = $this->model->scenarios();
  97. $this->attributeLabels = $this->model->attributeLabels();
  98. foreach ($this->safeAttributes() as $attribute) {
  99. $this->attributes[$attribute] = '';
  100. }
  101. }
  102. /**
  103. * @param ActiveQuery $query
  104. * @param string $attribute
  105. * @param bool $partialMath
  106. */
  107. private function addCondition($query, $attribute, $partialMath = false)
  108. {
  109. if (isset($this->relationAttributes[$attribute])) {
  110. $attributeName = $this->relationAttributes[$attribute];
  111. } else {
  112. $attributeName = call_user_func([$this->modelClassName, 'tableName']) . '.' . $attribute;
  113. }
  114. $value = $this->$attribute;
  115. if ($value === '') {
  116. return;
  117. }
  118. if ($partialMath) {
  119. $query->andWhere(['like', $attributeName, trim($value)]);
  120. } else {
  121. $query->andWhere($this->conditionTrans($attributeName, $value));
  122. }
  123. }
  124. /**
  125. * 可以查询大于小于和IN
  126. *
  127. * @param $attributeName
  128. * @param $value
  129. * @return array
  130. */
  131. private function conditionTrans($attributeName, $value)
  132. {
  133. switch (true) {
  134. case is_array($value):
  135. return [$attributeName => $value];
  136. break;
  137. case stripos($value, '>=') !== false:
  138. return ['>=', $attributeName, substr($value, 2)];
  139. break;
  140. case stripos($value, '<=') !== false:
  141. return ['<=', $attributeName, substr($value, 2)];
  142. break;
  143. case stripos($value, '<') !== false:
  144. return ['<', $attributeName, substr($value, 1)];
  145. break;
  146. case stripos($value, '>') !== false:
  147. return ['>', $attributeName, substr($value, 1)];
  148. break;
  149. case stripos($value, ',') !== false:
  150. return [$attributeName => explode(',', $value)];
  151. break;
  152. default:
  153. return [$attributeName => $value];
  154. break;
  155. }
  156. }
  157. /**
  158. * @return Model
  159. */
  160. public function getModel()
  161. {
  162. return $this->model;
  163. }
  164. /**
  165. * @param mixed $value
  166. */
  167. public function setModel($value)
  168. {
  169. if ($value instanceof Model) {
  170. $this->model = $value;
  171. $this->scenario = $this->model->scenario;
  172. $this->modelClassName = get_class($value);
  173. } else {
  174. $this->model = new $value;
  175. $this->modelClassName = $value;
  176. }
  177. }
  178. /**
  179. * @return array
  180. */
  181. public function rules()
  182. {
  183. return $this->rules;
  184. }
  185. /**
  186. * @return array
  187. */
  188. public function attributeLabels()
  189. {
  190. return $this->attributeLabels;
  191. }
  192. /**
  193. * @return array
  194. */
  195. public function scenarios()
  196. {
  197. return $this->scenarios;
  198. }
  199. /**
  200. * @param array $params
  201. * @return ActiveDataProvider
  202. */
  203. public function search($params)
  204. {
  205. $query = call_user_func([$this->modelClassName, 'find']);
  206. $dataProvider = new ActiveDataProvider(
  207. [
  208. 'query' => $query,
  209. 'pagination' => new Pagination(
  210. [
  211. 'forcePageParam' => false,
  212. 'pageSize' => $this->pageSize,
  213. ]
  214. ),
  215. ]
  216. );
  217. if (is_array($this->relations)) {
  218. foreach ($this->relations as $relation => $attributes) {
  219. $pieces = explode('.', $relation);
  220. $path = '';
  221. $parentPath = '';
  222. foreach ($pieces as $i => $piece) {
  223. if ($i == 0) {
  224. $path = $piece;
  225. } else {
  226. $parentPath = $path;
  227. $path .= '.' . $piece;
  228. }
  229. if (!isset($this->internalRelations[$path])) {
  230. if ($i == 0) {
  231. $relationClass = call_user_func([$this->model, 'get' . $piece]);
  232. } else {
  233. $className = $this->internalRelations[$parentPath]['className'];
  234. $relationClass = call_user_func([new $className, 'get' . $piece]);
  235. }
  236. $this->internalRelations[$path] = [
  237. 'className' => $relationClass->modelClass,
  238. 'tableName' => call_user_func([$relationClass->modelClass, 'tableName']),
  239. ];
  240. }
  241. }
  242. foreach ((array)$attributes as $attribute) {
  243. // $attributeName = str_replace('.', '_', $relation) . '_' . $attribute;
  244. $attributeName = $relation . '.' . $attribute;
  245. $tableAttribute = $this->internalRelations[$relation]['tableName'] . '.' . $attribute;
  246. $this->rules[] = [$attributeName, 'safe'];
  247. $this->scenarios[$this->scenario][] = $attributeName;
  248. $this->attributes[$attributeName] = '';
  249. $this->relationAttributes[$attributeName] = $tableAttribute;
  250. $dataProvider->sort->attributes[$attributeName] = [
  251. 'asc' => [$tableAttribute => SORT_ASC],
  252. 'desc' => [$tableAttribute => SORT_DESC],
  253. ];
  254. }
  255. }
  256. // 重新组合 rule, 移除自定义的验证器
  257. $rules = [];
  258. $builtInValidators = Validator::$builtInValidators;
  259. $validRule = array_keys($builtInValidators);
  260. foreach ($this->rules as $rule) {
  261. if (isset($rule[1]) && in_array($rule[1], $validRule)) {
  262. $rules[] = $rule;
  263. }
  264. }
  265. $this->rules = $rules;
  266. $query->joinWith(array_keys($this->relations));
  267. }
  268. if (is_array($this->defaultOrder)) {
  269. $dataProvider->sort->defaultOrder = $this->defaultOrder;
  270. }
  271. if (is_array($this->groupBy)) {
  272. $query->addGroupBy($this->groupBy);
  273. }
  274. $this->load($params);
  275. foreach ($this->attributes as $name => $value) {
  276. $this->addCondition($query, $name, in_array($name, $this->partialMatchAttributes));
  277. }
  278. return $dataProvider;
  279. }
  280. /**
  281. * @param string $name
  282. * @return mixed
  283. * @throws \yii\base\UnknownPropertyException
  284. */
  285. public function __get($name)
  286. {
  287. if (isset($this->attributes[$name])) {
  288. return $this->attributes[$name];
  289. }
  290. return parent::__get($name);
  291. }
  292. /**
  293. * @param string $name
  294. * @param mixed $value
  295. * @throws \yii\base\UnknownPropertyException
  296. */
  297. public function __set($name, $value)
  298. {
  299. if (isset($this->attributes[$name])) {
  300. $this->attributes[$name] = $value;
  301. } else {
  302. parent::__set($name, $value);
  303. }
  304. }
  305. }
粤ICP备19079148号