我正在使用yii2-advanced
。我有幾個表:
如何將相同的數據記錄插入Yii2上的多個表格
tb_user
:(iduser(PK)
,username
)tb_profile
:(id
,iduser(FK)
),tb_status
:(id
,iduser(FK)
)
我的問題我怎麼能插入iduser(PK)
從tb_user
到iduser(FK)
在tb_profile
和tb_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,
]);
}
你能提供你需要插入記錄的例子嗎? – arogachev
@arogachev on'tb_user'我有數據記錄('id','username')。現在,我想'tb_user'的記錄將被添加到'tb_profile'和'tb_status'中。 – Adi