donglin9068 2010-09-20 23:26
浏览 86
已采纳

Zend Framework - 使用WHERE和AND更新数据库行

To use where in a MySQL table row update in Zend Framework I have something like:

public function updateBySiteId(array $data, $id) {
        $table = $this->gettable();

        $where = $table->getAdapter()->quoteInto('site_id = ?', $id);

        return $table->update($data, $where);
    }

and this, I expect, gives me something like...

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1'

But what if I want to create the following:

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie'

In the manual I don't see how to do this with quoteInto (or quote or some other safe method... which could just mean I'm looking in the wrong place but... sigh).

  • 写回答

1条回答 默认 最新

  • douyouyi8878 2010-09-20 23:44
    关注

    Since the table update() method proxies to the database adapter update() method, the second argument can be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.

    http://framework.zend.com/manual/en/zend.db.table.html

    $data = array(
    'updated_on'      => '2007-03-23',
    'bug_status'      => 'FIXED'
    );
    $where[] = "reported_by = 'goofy'";
    $where[] = "bug_status = 'OPEN'";
    $n = $db->update('bugs', $data, $where);
    

    Resulting SQL is:

    UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED' WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
    

    http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置