Exemple #1
0
 public function search($params)
 {
     $query = User::find()->joinWith(['role' => function ($query) {
         $query->from(['role' => Role::tableName()]);
     }]);
     if ($this->load($params) && $this->validate()) {
         $query->andFilterWhere(['id' => $this->id])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'role.item_name', $this['role.item_name']]);
         if (preg_match('/[\\d]{2}-[\\d]{2}-[\\d]{4}/', $this->created_at)) {
             $query->andFilterWhere(['AND', ['>', 'user.created_at', \DateTime::createFromFormat('d-m-Y H:i:s', $this->created_at . ' 00:00:00')->getTimestamp()], ['<', 'user.created_at', \DateTime::createFromFormat('d-m-Y H:i:s', $this->created_at . ' 23:59:59')->getTimestamp()]]);
         }
     }
     $userProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['id', 'email', 'name', 'created_at' => ['asc' => ['user.created_at' => SORT_ASC], 'desc' => ['user.created_at' => SORT_DESC], 'default' => SORT_DESC], 'role.item_name'], 'defaultOrder' => ['created_at' => SORT_DESC]], 'pagination' => ['pageSize' => 10, 'defaultPageSize' => 10]]);
     return $userProvider;
 }
 public function actionUpdate($id)
 {
     $model = Notice::findOne($id);
     if ($model->load(Yii::$app->request->post())) {
         // 获取用户输入的数据,验证并保存
         $rids = $model->rids;
         $model->rids = ',' . implode(',', $model->rids) . ',';
         if ($model->save()) {
             $this->redirect('/notice/index');
         }
     }
     $rids = $model->rids;
     $model->rids = explode(',', $rids);
     $roles = Role::findBySql('SELECT * FROM ' . Role::tableName() . ' where id > 1')->all();
     return $this->render('update', array('model' => $model, 'roles' => $roles));
 }
 public function actionUpdate($id)
 {
     $model = User::findOne($id);
     if (empty($model)) {
         throw new HttpException(404, '用户不存在!');
     }
     if ($this->user->rid > 1 && $model->rid == 2) {
         throw new MethodNotAllowedHttpException('权限不够!');
     }
     if ($model->load(Yii::$app->request->post())) {
         // 获取用户输入的数据,验证并保存
         if ($model->save()) {
             $this->redirect('/user/index');
         }
     }
     $roles = Role::findBySql('SELECT * FROM ' . Role::tableName() . ' where id > 1')->all();
     return $this->renderAjax('update', array('model' => $model, 'roles' => $roles));
 }