我们可以在Zend-Db中执行查询时锁定表

dee*_*dav 0 zend-framework zend-db

我在谈论做这样的事情:

LOCK TABLE页面WRITE;

SELECT*FROM page WHERE col ='value';

INSERT INTO页面(col1,col2)VALUES('val1',val2);

解锁表;

Jak*_*son 8

我没有看到实际的Zend DB方法来锁定表,但可能只是这样做:

//Lock Table
$sql = "LOCK TABLE page WRITE";
$db->fetchRow($sql);

//Get your data
$sql = "SELECT * FROM page WHERE col='value'";
$result = $db->fetchAll($sql);

//Make the insert
$data = array( 'col1' => 'val1', 'col2' => 'val2' );
$db->insert('page', $data);

//Unlock tables
$sql = "UNLOCK TABLES";
$db->fetchRow($sql);
Run Code Online (Sandbox Code Playgroud)

可能不是最好的解决方案,而且未经测试.但它可能适用于你.

更新:我遇到了一个更好的解决方案.使用交易:

// Start a transaction explicitly.
$db->beginTransaction();

try {
    //Get your data
    $sql = "SELECT * FROM page WHERE col='value'";
    $result = $db->fetchAll($sql);
    //Make the insert
    $data = array( 'col1' => 'val1', 'col2' => 'val2' );
    $db->insert('page', $data);

    // If all succeed, commit the transaction and all changes
    // are committed at once.
    $db->commit();

} catch (Exception $e) {
    // If any of the queries failed and threw an exception,
    // we want to roll back the whole transaction, reversing
    // changes made in the transaction, even those that succeeded.
    // Thus all changes are committed together, or none are.
    $db->rollBack();
    echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)

我最近遇到了同样的问题,交易效果很好.绝对是要走的路.