简体   繁体   中英

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. Just don't know how to do db transaction in PHP Lithium framework.

Since Lithium uses PDO, you can just get the PDO object and call the beginTransaction() method.

$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

It doesn't seem like it is supported, unfortunately. See source of lithium/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 = 'SELECT * FROM users WHERE id = {:id}';

and $placeholders (optional) are your placeholders:

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

Using that knowledge, you should be able to set up a transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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