Yii Framework Forum: How to save multiple models while signing up - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to save multiple models while signing up

#1 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 23-November 16

Posted 21 March 2018 - 09:34 PM

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?
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,623
  • Joined: 16-February 11
  • Location:Japan

Posted 22 March 2018 - 06:57 AM

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,
        ]);
    }

0

#3 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 23-November 16

Posted 22 March 2018 - 08:04 AM

View Postsoftark, on 22 March 2018 - 06:57 AM, said:

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,
        ]);
    }


Thank you it is working 100%
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users