W
W
webquestions2016-03-06 13:02:53
Yii
webquestions, 2016-03-06 13:02:53

How to save data in Yii c form?

$model->save();
does not fulfill

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Natarov, 2016-03-06
@HanDroid

The order of actions should be in the form of making a function. You can assign your own values ​​​​by handles, but throw the admin and the test beam through migrations.

public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username; 
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->generateAuthKey();

            if ($user->save()) {
                   // тут можно закинуть отправку токена на мыло
            }
            return $user; // это отправка будет на ваш экшн
        }
        return null;
    }

in controller
public function actionSignup()
    {
        $model = new SignupForm(); // создается форма что выше
        if ($model->load(Yii::$app->request->post())) { // грузит модель из суперглобалки ПОСТ
            if ($user = $model->signup()) { // создается юзер используется функция из формы
                Yii::$app->getSession()->setFlash('success', 'Регистрация успешна'); // всплывающее сообщение 
                return $this->goHome(); //возврат на домашку
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

And the function model->save(); Hold in your IDE control and click on the save function, see how it works. This is a normal INSERT.
In Users, you create an array of data, not an object. At least the user needs a function that would pass this array to you. and in the controller you would use something like
$model = New User();
$model->username = $arrayUser['100']['username']
$model->email = $arrayUser['100']['email']
$model->setPassword($arrayUser['100']['password']);
$model->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question