/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Profile::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'fullname', $this->fullname])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'surname', $this->surname])->andFilterWhere(['like', 'patronymic', $this->patronymic])->andFilterWhere(['like', 'gender', $this->gender])->andFilterWhere(['like', 'address_id', $this->address_id]); return $dataProvider; }
public function actionProfile() { $model = $this->findModel(); $profile = Profile::find()->where(['user_id' => $model->id])->one(); Yii::setAlias('@upload', '@webroot/uploads/user/avatar/'); if (Yii::$app->request->isPost && !empty($_FILES)) { $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); $fileName = $model->id . '_' . time() . rand(1, 10000) . '.' . $extension; Image::thumbnail($_FILES['file']['tmp_name'], 160, 160)->save(Yii::getAlias('@upload') . $fileName, ['quality' => 80]); //delete old icon $file_exists = file_exists(Yii::getAlias('@upload') . $model->avatar); if ($file_exists && strpos($model->avatar, 'default') === false) { @unlink(Yii::getAlias('@upload') . $model->avatar); } $model->avatar = $fileName; $model->update(); } if ($profile->load(Yii::$app->request->post()) && $profile->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Saved successfully')); } return $this->render('profile', ['profile' => $profile, 'model' => $model]); }