在zend框架中使用LIMIT进行更新

时间:2012-05-23 14:44:01

标签: php mysql zend-framework

  

可能重复:
  How do I add a limit to update-query in Zend Framework?

我想进行更新并将LIMIT 1作为预防措施。 这是我的代码:

$vals = array();
$vals['status'] = 'closed';
$where = $write->quoteInto('id =?', $orderID);

$write->update("order", $vals ,$where);

我可以在哪里向此查询添加LIMIT?

(看起来过去曾被问过,但我希望那里有人有答案)

3 个答案:

答案 0 :(得分:1)

嗯......我的Zend生锈了,但我想我过去曾使用过:http://framework.zend.com/manual/en/zend.db.statement.html执行SQL(选择/插入/更新)作为普通的sql语句......

但是,所有旧的和当前的信息都是正确的 - update()无法设置'限制' - 你只需要希望你正确设计数据库,这样的表不会有重复的密钥!

另外......'order'是一个非常糟糕的表名:),'order'是MySQL中的保留字(确实是任何数据库!)。

答案 1 :(得分:1)

看起来你正在使用Zend_Db_Adapter来执行查询,所以我不确定你能做我做的事情,无论如何这里都是。
我通常使用Zend_Db_Table_Row save()方法来插入和更新记录,但是我也使用DbTable模型来提供对Table和Table_Row抽象API的访问。

public function save(Music_Model_Artist $artist) {
        //if id is not null
        if (!is_null($artist->id)) {
            //find row, uses fetchRow() so will return NULL if not found
            $row = $this->findById($artist->id);
            $row->id = $artist->id;
        } else {
            //if ID is null create new row
            $row = $this->_getGateway()->createRow();
        }
        //assign data to row
        $row->name = $artist->name;
        //save new row or update
        $row->save();
        //return the row, in case I need it for something else
        return $row;
    }

这相当于如果我在数据数组中包含和ID(或者在这种情况下是实体对象),则行将被更新,如果对象中没有ID,则将创建新行。 />

如果你好奇这就是我__构建课程的方式:

public function __construct(Zend_Db_Table_Abstract $tableGateway = NULL) {
        //pass in concrete DbTable Model, parent alts to using table name string to construct class
        $this->_tableGateway = new Application_Model_DbTable_Artist();
        parent::__construct($tableGateway);
    }

希望这能提供一些帮助。

答案 2 :(得分:0)

好吧,看起来没有办法做到这一点我只是决定使用straingt查询功能。我仍然使用where build功能,从WHERE中删除任何有趣的不需要的值。

$where = $write->quoteInto('id =?', $order_id);
$write->query("UPDATE orders SET status = 'closed' WHERE $where LIMIT 1");