我正在使用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