Greetings. I do edit the user profile, saving does not work. I found out experimentally that reading form fields in the model properties ( $model->load(Yii::$app->request->post()) ) does not work. Please help find out why. So, I give the code:
(I don’t cite all use and namespace - they are there, leaving only the essence)
UserController.php (контроллер) public function actionProfile() { $model = new ProfileForm(); // создаем объект модели с формой ProfileForm if ($model->load(Yii::$app->request->post())) { // вот тут условие проходит, но функция не срабатывает - $model не содержит полей из формы if ($user = $model->update()) { return $this->redirect('/user/profile'); } } return $this->render('profile', [ 'model' => $model ]); } ProfileForm.php (form model)
class ProfileForm extends Model { public $username; public $email; public $firstname; public $middlename; public $lastname; public function update() { $user = \Yii::$app->user->identity; $user->country = $this->username; $user->country = $this->email; $user->firstname = $this->firstname; $user->middlename = $this->middlename; $user->lastname = $this->lastname; return $user->save() ? $user : null; } } profile.php (view profile forms)
$user = \Yii::$app->user->identity; <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'username')->label('Имя пользователя')->textInput(['value' => $user->username, 'readonly' => true]) ?> <?= $form->field($model, 'email')->label('Ваш e-mail')->textInput(['value'=>$user->email, 'readonly' => true]) ?> <?= $form->field($model, 'firstname')->label('Имя')->textInput(['value'=>$user->firstname]) ?> <?= $form->field($model, 'middlename')->label('Отчество')->textInput(['value'=>$user->middlename]) ?> <?= $form->field($model, 'lastname')->label('Фамилия')->textInput(['value'=>$user->lastname]) ?> <?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> <?php ActiveForm::end(); ?> The object \Yii:$app->user->identity is implemented in the Users model. There is nothing interesting here (just in case):
<?php namespace frontend\models; use yii\db\ActiveRecord; use yii\web\IdentityInterface; class Users extends ActiveRecord implements IdentityInterface { public static function tableName() { return 'users'; } public function setPassword($password) { $this->password_hash = md5($password); } public function validatePassword($password) { return $this->password_hash === md5($password); } public static function findIdentity($id) { return self::findOne($id); } public function getId() { return $this->id; } public static function findIdentityByAccessToken($token, $type = null) { } public function getAuthKey() { } public function validateAuthKey($authKey) { } } So, by experience, I found out that the problem is in the controller (at the very beginning):
if ($model->load(Yii::$app->request->post())) { condition is triggered. But as far as I understand, the $model = load($post_data) must read the elements of the $post_data and fill them in the $model array, provided that both elements have the same indices.
That is, it was:
['username' => 'тут старые данные из базы'], ['email' => 'тут старые данные из базы'], ['firstname' => 'тут старые данные из базы'], ['middlename' => 'тут старые данные из базы'], ['lastname' => 'тут старые данные из базы'], Yii::$app->request->post() [ ['username' => 'тут новые данные из формы'], ['email' => 'тут новые данные из формы'], ['firstname' => 'тут новые данные из формы'], ['middlename' => 'тут новые данные из формы'], ['lastname' => 'тут новые данные из формы'], ] After if ($model->load(Yii::$app->request->post())) should be:
$model [ ['username' => 'тут новые данные из формы'], ['email' => 'тут новые данные из формы'], ['firstname' => 'тут новые данные из формы'], ['middlename' => 'тут новые данные из формы'], ['lastname' => 'тут новые данные из формы'], ] But the $model data is not updated. I have been looking for examples of using the load() function for a long time, and it seems to me that I understood correctly how it works. But why then does not save occur? When creating a new user, I did the same and everything worked, and when editing it does not work. Please help. Thank you in advance.