MerchantSelect.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\enums\StatusEnum;
  6. use common\models\merchant\Merchant;
  7. /**
  8. * Trait MerchantSelectAction
  9. * @package common\traits
  10. */
  11. trait MerchantSelect
  12. {
  13. /**
  14. * select2 查询
  15. *
  16. * @param null $q
  17. * @param null $id
  18. * @return array
  19. */
  20. public function actionSelect2($q = null, $id = null)
  21. {
  22. Yii::$app->response->format = Response::FORMAT_JSON;
  23. $out = [
  24. 'results' => [
  25. 'id' => '',
  26. 'text' => ''
  27. ]
  28. ];
  29. if (!is_null($q)) {
  30. $data = Merchant::find()
  31. ->select('id, title as text')
  32. ->where(['like', 'title', $q])
  33. ->andWhere(['status' => StatusEnum::ENABLED])
  34. ->limit(10)
  35. ->asArray()
  36. ->all();
  37. $out['results'] = array_values($data);
  38. } elseif ($id > 0) {
  39. $out['results'] = ['id' => $id, 'text' => Merchant::findOne($id)->mobile];
  40. }
  41. return $out;
  42. }
  43. }
粤ICP备19079148号