SearchModel.php 9.3 KB

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