2015-10-19 93 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我現在明白了你的觀點,但是我仍然建議你採用所有方法,現在你可以使用任何適合你情況的方法。 –

相關問題