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

Lot*_*har 5 php zend-framework

where在Zend Framework中的MySQL表行更新中使用,我有类似的东西:

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

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

        return $table->update($data, $where);
    }
Run Code Online (Sandbox Code Playgroud)

而且,我希望,这给我一些像......

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1'
Run Code Online (Sandbox Code Playgroud)

但是如果我想创建以下内容呢?

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie'
Run Code Online (Sandbox Code Playgroud)

在手册中我没有看到如何使用quoteInto(或引用或其他一些安全的方法......这可能只是意味着我在错误的地方但是...... 叹息).

Gal*_*len 9

由于表update()方法代理数据库适配器update()方法,因此第二个参数可以是SQL表达式的数组.表达式使用AND运算符组合为布尔项.

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);
Run Code Online (Sandbox Code Playgroud)

产生的SQL是:

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

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

  • 这样做了.我在手册中读到了这一点,但是错误地创建了数组:$ where = $ table-> getAdapter() - > quoteInto(array('site_id =?'=> $ id,'foo'=> $ bar)); (2认同)