26

通常,这对我有用:

$db = Zend_Db_Table::getDefaultAdapter();
$where = $db->quoteInto('id = ?', $id);
$db->delete('tablename', $where);

但我必须匹配两个 id。所以我真的不知道如何构造它。

WHERE first_id = 'id1' AND second_id = 'id2'

那么如何使用 Zend 框架做到这一点呢?

4

3 回答 3

59

扩展 Jason W 的回答:

不完全确定第 3 部分在说什么

这意味着你可以这样做:

$db->delete('tablename', array(
    'first_id = ?' => $first_id,
    'second_id = ?' => $second_id
));

适配器将为您引用所有内容。

我觉得文档不是很清楚。

于 2009-12-04T08:07:40.640 回答
20

来自关于 delete()的zend 手册:

如果省略第二个参数,则结果是数据库表中的所有行都被删除。

如果您提供一个字符串数组作为第二个参数,则这些字符串将作为由 AND 运算符分隔的表达式中的项连接在一起。

如果您提供数组数组作为第二个参数,则值将自动引用到键中。然后这些将作为术语连接在一起,由 AND 运算符分隔。

不完全确定第 3 部分在说什么,但第 2 部分暗示您可以执行以下操作:

$where = array();
$where[] = $db->quoteInto('first_id = ?', $first_id);
$where[] = $db->quoteInto('second_id = ?', $second_id);
$db->delete('tablename', $where);
于 2009-12-04T06:32:18.190 回答
1

如果您在扩展类 Zend_Db_Table_Abstract 的模型中,则需要使用不同的结构:

class Yourmodel extends Zend_Db_Table_Abstract {
    protected $_name = 'tablename';
    protected $_primary = 'primarykey';
    public function remove($first_id, $second_id) {
        $where = array();
        $where[] = $this->getAdapter()->quoteInto('first_id = ?', $first_id);
        $where[] = $this->getAdapter()->quoteInto('second_id = ?', $second_id);
        $this->delete($where);
    }
}
于 2013-09-03T09:58:50.633 回答