Commit 658a5f37537813f0f9b3d3243ccebee7fc468698
1 parent
3c470529
test
Showing
4 changed files
with
130 additions
and
6 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\models; | ||
| 4 | + | ||
| 5 | + use Yii; | ||
| 6 | + use yii\base\Model; | ||
| 7 | + use yii\data\ActiveDataProvider; | ||
| 8 | + use common\models\User; | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * CustomerSearch represents the model behind the search form about `common\models\UserInfo`. | ||
| 12 | + */ | ||
| 13 | + class CustomerSearch extends User | ||
| 14 | + { | ||
| 15 | + | ||
| 16 | + public $type; | ||
| 17 | + | ||
| 18 | + public $rating; | ||
| 19 | + | ||
| 20 | + public $online; | ||
| 21 | + | ||
| 22 | + public $city; | ||
| 23 | + | ||
| 24 | + public $info; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * @inheritdoc | ||
| 28 | + */ | ||
| 29 | + public function rules() | ||
| 30 | + { | ||
| 31 | + return [ | ||
| 32 | + [ | ||
| 33 | + [ | ||
| 34 | + 'type', | ||
| 35 | + 'rating', | ||
| 36 | + 'online', | ||
| 37 | + ], | ||
| 38 | + 'integer', | ||
| 39 | + ], | ||
| 40 | + [ | ||
| 41 | + [ | ||
| 42 | + 'city', | ||
| 43 | + 'info', | ||
| 44 | + ], | ||
| 45 | + 'safe', | ||
| 46 | + ], | ||
| 47 | + ]; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * @inheritdoc | ||
| 52 | + */ | ||
| 53 | + public function scenarios() | ||
| 54 | + { | ||
| 55 | + // bypass scenarios() implementation in the parent class | ||
| 56 | + return Model::scenarios(); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Creates data provider instance with search query applied | ||
| 61 | + * | ||
| 62 | + * @param array $params | ||
| 63 | + * | ||
| 64 | + * @return ActiveDataProvider | ||
| 65 | + */ | ||
| 66 | + public function search($params) | ||
| 67 | + { | ||
| 68 | + $query = User::find() | ||
| 69 | + ->joinWith('userInfo') | ||
| 70 | + ->joinWith('companyInfo'); | ||
| 71 | + | ||
| 72 | + $dataProvider = new ActiveDataProvider([ | ||
| 73 | + 'query' => $query, | ||
| 74 | + ]); | ||
| 75 | + | ||
| 76 | + $this->load($params); | ||
| 77 | + | ||
| 78 | + if(!$this->validate()) { | ||
| 79 | + // uncomment the following line if you do not want to return any records when validation fails | ||
| 80 | + // $query->where('0=1'); | ||
| 81 | + return $dataProvider; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + $query->andWhere([ | ||
| 85 | + 'is_customer' => 1, | ||
| 86 | + ]); | ||
| 87 | + | ||
| 88 | + if($this->type == 2) { | ||
| 89 | + $query->andWhere([ | ||
| 90 | + 'not', | ||
| 91 | + [ 'company_info.company_info_id' => NULL ], | ||
| 92 | + ]); | ||
| 93 | + | ||
| 94 | + $query->andWhere([ | ||
| 95 | + 'type' => 2, | ||
| 96 | + ]); | ||
| 97 | + } else { | ||
| 98 | + $query->andWhere([ | ||
| 99 | + 'type', | ||
| 100 | + ]); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + if($this->online == 1) { | ||
| 104 | + $query->andWhere([ | ||
| 105 | + '>=', | ||
| 106 | + 'user_info.date_visit', | ||
| 107 | + date('Y-m-d H:i:s.u', time() - 1800), | ||
| 108 | + ]); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + $query->andFilterWhere([ | ||
| 112 | + 'like', | ||
| 113 | + 'user_info.city', | ||
| 114 | + $this->city, | ||
| 115 | + ]); | ||
| 116 | + | ||
| 117 | + return $dataProvider; | ||
| 118 | + } | ||
| 119 | + } |
common/models/User.php
frontend/controllers/SearchController.php
| @@ -68,6 +68,7 @@ class SearchController extends Controller | @@ -68,6 +68,7 @@ class SearchController extends Controller | ||
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | public function actionCustomer(){ | 70 | public function actionCustomer(){ |
| 71 | + | ||
| 71 | return $this->render('customer',[ | 72 | return $this->render('customer',[ |
| 72 | ]); | 73 | ]); |
| 73 | } | 74 | } |
frontend/views/search/customer.php
| 1 | <?php | 1 | <?php |
| 2 | -/** | ||
| 3 | - * Created by PhpStorm. | ||
| 4 | - * User: vitaliy | ||
| 5 | - * Date: 09.03.16 | ||
| 6 | - * Time: 10:21 | ||
| 7 | - */ | 2 | + use common\models\CustomerSearch; |
| 3 | + | ||
| 4 | + $model = (new CustomerSearch())->search(\Yii::$app->request->queryParams); | ||
| 5 | + var_dump($model->totalCount); | ||
| 6 | + | ||
| 8 | ?> | 7 | ?> |
| 9 | <div class="section-box-22 section-box-customer"> | 8 | <div class="section-box-22 section-box-customer"> |
| 10 | <div class="box-wr"> | 9 | <div class="box-wr"> |