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

5
为了在Zend Framework中使用where更新MySQL表行,我有以下代码:
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'

但是如果我想创建以下内容怎么办:
UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie'

在手册中,我没有看到如何使用quoteInto(或quote或其他安全的方法)来完成这个任务...可能是因为我没有找对地方,但是...

1个回答

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);

生成的 SQL 语句为:

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


2
搞定了。我在手册中读到了那一部分,但是数组创建错误了:$where = $table->getAdapter()->quoteInto(array('site_id = ?' => $id, 'foo' => $bar)); - Lothar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接