简体   繁体   English

Lithium PHP框架-如何在m​​ysql上运行数据库事务?

[英]Lithium PHP framework - How to run db transaction on mysql?

I have been working on an e-commerce website in PHP Lithium framework, actually this one is upgraded from CakePHP, we have to use transaction operation on db in mysql. 我一直在PHP Lithium框架上的一个电子商务网站上工作,实际上这是从CakePHP升级的,我们必须在mysql中的db上使用事务操作。 Just don't know how to do db transaction in PHP Lithium framework. 只是不知道如何在PHP Lithium框架中执行数据库事务。

Since Lithium uses PDO, you can just get the PDO object and call the beginTransaction() method. 由于Lithium使用PDO,因此您只需获取PDO对象并调用beginTransaction()方法即可。

$foo = app\models\Foo::create();
$pdo = Connections::get('default')->connection; 
$pdo->beginTransaction();
$foo->bar = 'Hello';
$foo->save();
$pdo->commit();

https://github.com/UnionOfRAD/lithium/issues/1004#issuecomment-23690165 http://www.php.net/manual/en/pdo.begintransaction.php https://github.com/UnionOfRAD/lithium/issues/1004#issuecomment-23690165 http://www.php.net/manual/zh/pdo.begintransaction.php

It doesn't seem like it is supported, unfortunately. 不幸的是,它似乎不受支持。 See source of lithium/data/source/database/adapter/MySql.php . 参见锂的来源/data/source/database/adapter/MySql.php

An alternative may be to manually execute your queries. 另一种选择是手动执行查询。

Within your model, you can do: 在模型中,您可以执行以下操作:

static::connection->read($sql, $placeholders);

Where $sql is your raw SQL like: 其中$sql是您的原始SQL,例如:

$sql = 'SELECT * FROM users WHERE id = {:id}';

and $placeholders (optional) are your placeholders: $placeholders (可选)是您的占位符:

$placeholders = [
    'id' => 5
];

Using that knowledge, you should be able to set up a transaction. 使用该知识,您应该能够进行交易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM