如何在 Yii2 上向多个 table 插入相同的数据记录

How to insert same data record to multiple table on Yii2

我正在使用 yii2-advanced。我有几个 table :

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

我的问题是如何在按下注册按钮后在 tb_profiletb_status 上将 iduser(PK)tb_user 插入到 iduser(FK)
有一段时间我想我必须对 User 模型上的 bevahiours() 函数做一些修改,我发现了一些错误,或者在 table 上添加 trigger 语法? (我认为这不是一个好方法)。
有没有人可以帮助我,如何解决我的问题?

这是修改前的User模型:

<?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();
    }

}
?>

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 个 table,例如 useruser_image,其中 user_id 是添加路径的外键。

对于这种情况,您可以使用以下任一方法

1.Insert 通过点击 signup 按钮在两个 table 中记录。您将必须相应地编写更新操作。

 $user = new User();
 $user->name = "John"
 $user->email = "John@gmail.com"
 //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 也可以调用 UserImagecreate 动作,做同样的事情。如果您使用这种方法,那么您可能还需要使用任何其他唯一列来查找该用户的 id 并使用它来插入新记录,例如在我的 table email 中独特的列,所以我可以在 UserImage 中编写以下代码并获得 id

$user = User::findOne(['email' => 'john@gmail.com']);//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

这样您就可以根据需要使用代码

谢谢