1

我有一个事务,我在 TableB 中插入一条新记录,如果 TableA 中没有适当的支持记录,我也将插入到 TableA 中并将该主键用作 TableB 的外键条目。

在此处输入图像描述

$transaction=$connection->beginTransaction();
try 
{
    $tableA->IsActive = 'Y'; 
    $tableA->save();

    $model->TableAId = $tableA->TableAId;  //not sure what to put here for $tableA->TableAId
    $model->save();

    $transaction->commit(); 
}

在这种情况下,我可以Yii::app()->db->getLastInsertId();使用$tableA->TableAId;

显然准确性在这里非常重要,所以我需要保证正确的记录在 TableA 和 TableB 之间对齐

4

1 回答 1

2

基本上,您的评论看起来很完美。$model->TableAId = $tableA->TableAId; // 太棒了!!但代码不是。以下应该是您的代码。

$transaction=$connection->beginTransaction();
try 
{
    $tableA = TableA::model()->findByPk("id"); OR $tableA = new TableA;
    $tableA->IsActive = 'Y'; 
    $tableA->save();

    $model = new TableB;
    $model->TableAId = $tableA->TableAId;  //not sure what to put here for $tableA->TableAId // This is perfect!!
    $model->save();

    $transaction->commit(); 
}
于 2012-12-12T06:40:59.227 回答