How to save multiple models while signing up


(Arunwebber) #1

I want to create profile data as well while signing up.

I have a table of profile and user, signing up is working fine. I can easily save sign up data now I want to create a profile for same user id I have created profile model with gii tools. Which looks like this.


<?php


namespace frontend\models;


use Yii;


/**

 * This is the model class for table "profile".

 *

 * @property int $user_id

 * @property string $name

 * @property string $public_email

 * @property string $gravatar_email

 * @property string $gravatar_id

 * @property string $location

 * @property string $website

 * @property string $bio

 * @property string $timezone

 *

 * @property User $user

 */

class Profile extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'profile';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['user_id'], 'required'],

            [['user_id'], 'integer'],

            [['bio'], 'string'],

            [['name', 'public_email', 'gravatar_email', 'location', 'website'], 'string', 'max' => 255],

            [['gravatar_id'], 'string', 'max' => 32],

            [['timezone'], 'string', 'max' => 40],

            [['user_id'], 'unique'],

            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'user_id' => 'User ID',

            'name' => 'Name',

            'public_email' => 'Public Email',

            'gravatar_email' => 'Gravatar Email',

            'gravatar_id' => 'Gravatar ID',

            'location' => 'Location',

            'website' => 'Website',

            'bio' => 'Bio',

            'timezone' => 'Timezone',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getUser()

    {

        return $this->hasOne(User::className(), ['id' => 'user_id']);

    }

}

Now I want to create an empty data of this same user in my DB while logging in I called this model in the sign up form which looks like this.


  public function actionSignup()

    {

        $model = new SignupForm();

        $model2 = new Profile();

        $authItems = AuthItem::find()->all();

        //print_r($authItems);

        if ($model->load(Yii::$app->request->post())) {

            if ($user = $model->signup() ) {




                if (Yii::$app->getUser()->login($user)) {

                    return $this->goHome();

                }

            }

        }


        return $this->render('signup', [

            'model' => $model,

            'authItems' => $authItems,

        ]);

    }

Now how can i create an empty profile record for the same user id while signing up?


(Softark) #2

Hi arunwebber,

SignupForm::signup() returns User object on success, so you can get the id of it for its profile.




public function actionSignup()

    {

        $model = new SignupForm();

        $model2 = new Profile();

        $authItems = AuthItem::find()->all();

        //print_r($authItems);

        if ($model->load(Yii::$app->request->post())) {

            if ($user = $model->signup() ) {

                $model2->user_id = $user->id;

                $model2->save();

                if (Yii::$app->getUser()->login($user)) {

                    return $this->goHome();

                }

            }

        }


        return $this->render('signup', [

            'model' => $model,

            'authItems' => $authItems,

        ]);

    }




(Arunwebber) #3

Thank you it is working 100%