3

I'm using yii2-advanced. I've several table :

  1. tb_user:(iduser(PK),username),
  2. tb_profile:(id,iduser(FK)),
  3. tb_status:(id,iduser(FK))

My question is how can i insert iduser(PK) from tb_user to iduser(FK) on tb_profile and tb_status after i push the signup button.
For a while i think i must to do some modification of bevahiours() function on User model and i found some error, or adding trigger syntax on the table ? (i think this is not a good ways).
Is there anyone who can help me, how to solve my problem ?

this is the User model before the modification :

<?php
namespace common\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;


class User extends ActiveRecord implements IdentityInterface
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
                'value' => function () {return date('Y-m-d h:m:s');},
            ],

        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

}
?>

The Controller :

public function actionSignup()
{
    $model = new SignupForm();

    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,
    ]);
}
2
  • Could you provide an example of record that you need to insert? Commented Oct 19, 2015 at 3:27
  • @arogachev on tb_user i've data record (id,username). Now, i want the record of tb_user will be added in tb_profile and tb_status. Commented Oct 19, 2015 at 3:50

1 Answer 1

2

I had similar situation in one of my project where i had 2 tables like user,user_image where user_id was foreign key to add the path.

For those kind of situation you can use either of following approach

1.Insert record in both table on click of signup button. You will have to write update action accordingly.

 $user = new User();
 $user->name = "John"
 $user->email = "[email protected]"
 //Add if any other fields in table
 $user->save(); //save the record

 $user_image = new UserImage();
 $user_image->user_id = $user->id;
 $user_image->image = "image path"
 //Add any other images here
 $user_image->save();//save the record

2.You can also call create action of UserImage and do the same. If you use this approach than you might also need to use any other unique column to find the id of that user and use it to insert new record,for example in my table email is unique column so i can write following code in UserImage and get the id

$user = User::findOne(['email' => '[email protected]']);//this will return whole row 
$user_image->user_id = $user->id;
 $user_image->image = "image path"
 //Add any other images here
 $user_image->save();//save the record

And that way you can use the code as per it suits your need

Thank you

Sign up to request clarification or add additional context in comments.

8 Comments

how if i want to insert id as foreign key in related table ? what should i do ?
@Adi updated the answer. i hope thats what you want to do because i am still not clear what you wants to do
@Adi i got your point now but still i suggested all approaches to you now you can use any of those which fits your situation.
with this signup case of yii2-advanced I still do not understand, in which part of my file should I change ? SiteController file with signup function ? or User model file ?
@Adi i have updated the answer and yeah you need to change in SiteController action for signup. I dont know much about the structure of your project but if the url is web/site/signup than you need to change code in SiteController
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.