2015-10-19 94 views
3

我正在使用yii2-advanced。我有几个表:
如何将相同的数据记录插入Yii2上的多个表格

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

我的问题我怎么能插入iduser(PK)tb_useriduser(FK)tb_profiletb_status我按下注册按钮后。
有一段时间,我想我必须做一些bevahiours()函数User模型的修改,我发现一些错误,或在表上添加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, 
    ]); 
} 
+0

你能提供你需要插入记录的例子吗? – arogachev

+0

@arogachev on'tb_user'我有数据记录('id','username')。现在,我想'tb_user'的记录将被添加到'tb_profile'和'tb_status'中。 – Adi

回答

2

我有类似的情况在我的项目,我有2个表像useruser_image其中user_id是一个外键添加路径。

对于那种情况下,你可以使用signup按钮的点击下面的方法

1.Insert记录在这两个表中任一个。您必须相应地编写更新操作。

$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也可以调用的UserImagecreate行动,做同样的。如果你使用这种方法比你也可能需要使用任何其他独特的列来找到该用户的id并使用它来插入新记录,例如在我的表email是唯一的列,所以我可以在UserImage中写下面的代码并获得您可以使用代码按它适合你的需要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 

而且这样

谢谢

+0

如何在外表中插入'id'作为外键?我该怎么办 ? – Adi

+0

@Adi更新了答案。我希望那就是你想做的事情,因为我仍然不清楚你想做什么 –

+0

@Adi我现在明白了你的观点,但是我仍然建议你采用所有方法,现在你可以使用任何适合你情况的方法。 –

相关问题