主题来自:http://www.yiiframework.com/forum/index.php/topic/47081-transaction-is-not-working/
您好!
我在行动中使用交易:
public function actionCreate()
{
$model_1 = new Model_1('somescenario');
$model_2 = new Model_2('somescenario2');
$model_3 = new Model_3();
$model_4 = new Model_4('somescenario3');
$model_5 = new Model_5();
$transaction = $model_1->getDbConnection()->beginTransaction();
try
{
if(isset($_POST['Model_1'], $_POST['Model_3'], $_POST['Model_3'],
$_POST['Model_4']))
{
$model_1->attributes = $_POST['Model_1'];
$model_2->attributes = $_POST['Model_2'];
$model_3->attributes = $_POST['Model_3'];
$model_4->attributes = $_POST['Model_4'];
$validate = $model_1->validate();
$validate = $model_2->validate() && $validate;
$validate = $model_3->validate() && $validate;
$validate = $model_4->validate() && $validate;
$validate = $model_5->validate() && $validate;
if($model_1->save())
{
$model_4->id_tb_1 = $model_1->id;
$model_2->id_tb_1 = $model_1->id;
$model_3->id_tb_1 = $model_1->id;
if($model_4->save() && $model_2->save() && $model_3->save())
{
if(isset($_POST['model_5']))
{
foreach($_POST['model_5']
['id'] as $id)
{
$model_6 = new Model_6();
$model_6->id = $id;
$model_6->id = $model_1->model_1_relation->id;
$model_6->save();
}
}
$model_2->createDatabaseRole($model_2->username);
$transaction->commit();
$this->redirect(array('userIndex'));
}
else
throw new CHttpException(404,
Yii::t('message', 'The specified model cannot be saved'));
}
else
throw new CHttpException(404,
Yii::t('message', 'The specified model cannot be saved'));
}
else
throw new CHttpException(404,
Yii::t('message', 'The specified POST cannot be found'));
}
catch(Exception $e)
{
$transaction->rollback();
}
}
但如果我在$ model_1-> save()之后生成异常,仍然在数据库中的$ model_1中的数据。我生成了一个异常并查看了数据库日志。它说交易开始并回滚:
LOG: statement: BEGIN
LOG: statement: SET NAMES 'utf8'
LOG: statement: SELECT 1 FROM "tb_2" "t" WHERE "t"."username"='thejames' LIMIT 1
LOG: statement: SET NAMES 'utf8'
LOG: statement: INSERT INTO "tb_1" ("...", "...", "...", "...", "...", "...", "...") VALUES ('The James', 'The James Bond', NULL, NULL, NULL, NULL, NULL)
LOG: execute <unnamed>: SELECT CURRVAL($1)
DETAIL: parameters: $1 = 'seq_tb_1'
LOG: statement: SET NAMES 'utf8'
LOG: statement: INSERT INTO "tb_4" ("...", "...", "...", "...", "...", "...", "...", "id") VALUES (NULL, '..', NULL, NULL, NULL, NULL, NULL, 0)
ERROR: insert or update on table "tb_4" violates foreign key constraint "tb_4_id_fkey"
DETAIL: Key (id)=(0) is not present in table "tb_1".
STATEMENT: INSERT INTO "tb_4" ("...", "...", "...", "...", "...", "...", "...", "id_1") VALUES (NULL, 'SP', NULL, NULL, NULL, NULL, NULL, 0)
LOG: statement: ROLLBACK
在WebLogRouter处,Model_1的插入在事务块内部开始:
12:24:02.860594 trace system.db.CDbConnection
Starting transaction
in /var/www/drmais/protected/controllers/C1Controller.php (59)
in /var/www/drmais/index.php (13)
12:24:02.860697 trace system.db.CDbConnection
Opening DB connection
in /var/www/drmais/protected/controllers/C1Controller.php (59)
in /var/www/drmais/index.php (13)
12:24:02.886655 trace system.CModule
Loading "messages" application component
in /var/www/drmais/protected/models/Model_1.php (75)
in /var/www/drmais/protected/controllers/C1Controller.php (66)
in /var/www/drmais/index.php (13)
12:24:02.899287 trace system.db.ar.CActiveRecord
User.exists()
in /var/www/drmais/protected/controllers/C1Controller.php (72)
in /var/www/drmais/index.php (13)
12:24:02.901856 trace system.db.CDbConnection
Opening DB connection
in /var/www/drmais/protected/controllers/C1Controller.php (72)
in /var/www/drmais/index.php (13)
12:24:02.927841 trace system.db.CDbCommand
Querying SQL: SELECT 1 FROM "tb_user" "t" WHERE "t"."username"=:ycp0 LIMIT
1
in /var/www/drmais/protected/controllers/C1Controller.php (72)
in /var/www/drmais/index.php (13)
12:24:02.932885 trace system.CModule
Loading "coreMessages" application component
in /var/www/drmais/protected/controllers/C1Controller.php (73)
in /var/www/drmais/index.php (13)
12:24:02.940749 trace system.db.ar.CActiveRecord
Model_1.insert()
in /var/www/drmais/protected/controllers/C1Controller.php (77)
in /var/www/drmais/index.php (13)
12:24:02.941178 trace system.db.CDbConnection
Opening DB connection
in /var/www/drmais/protected/controllers/C1Controller.php (77)
in /var/www/drmais/index.php (13)
12:24:02.966751 trace system.db.CDbCommand
Executing SQL: INSERT INTO "tb_1" ("...", "...",
"...", "...", "...", "...", "...")
VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)
in /var/www/drmais/protected/controllers/C1Controller.php (77)
in /var/www/drmais/index.php (13)
12:24:03.27409 trace system.db.ar.CActiveRecord
Model_4.insert()
in /var/www/drmais/protected/controllers/C1Controller.php (84)
in /var/www/drmais/index.php (13)
12:24:03.28028 trace system.db.CDbConnection
Opening DB connection
in /var/www/drmais/protected/controllers/C1Controller.php (84)
in /var/www/drmais/index.php (13)
12:24:03.55090 trace system.db.CDbCommand
Executing SQL: INSERT INTO "tb_4" ("...", "...", "...",
"...", "...", "...", "...", "...") VALUES
(:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7)
in /var/www/drmais/protected/controllers/C1Controller.php (84)
in /var/www/drmais/index.php (13)
12:24:03.68584 error system.db.CDbCommand SQLSTATE[23503]: Foreign key
violation: 7 ERROR: insert or update on table "tb_4" violates
foreign key constraint "tb_4_id_fkey"
DETAIL: Key (id)=(0) is not present in table "tb_1".. : INSERT INTO "tb_4" ("...",
"...", "...", "...", "...", "...", "...",
"id_tb_1") VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7).
in /var/www/drmais/protected/controllers/C1Controller.php (84)
in /var/www/drmais/index.php (13)
12:24:03.84001 trace system.db.CDbTransaction
Rolling back transaction
in /var/www/drmais/protected/controllers/C1Controller.php (116)
in /var/www/drmais/index.php (13)
此外,数据库日志仅报告一次插入。