gpt4 book ai didi

php - 从表中删除会导致违反基数

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:07 25 4
gpt4 key购买 nike

我有一个方法可以使用 zend 框架提供的 sql 适配器从数据库中删除行。

class Table extends Zend_Db_Table_Abstract {

...

$where = $this->getAdapter()->quoteInto(
'ModID=? AND URL=?',
array((int)$mid, $url->toString())
);
$this->delete($where);

问题是这段代码给出了错误:

Zend_Db_Statement_Exception: SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

在 where 子句中仅指定一列的类似代码工作正常。

是否有其他方法可以根据多列的值删除行?

最佳答案

你可以做这样的事情来利用 Zend_Db_Select 已经构建的功能:

$select = $this->select();
->where('modID = ?', (int)$mid);
->where('URL = ?', $url->toString());

$where = $select->getPart(Zend_Db_Select::WHERE);

$this->delete($where);

关于此解决方案的一些注意事项:

  • (专业版)利用 Zend_Db_Select 中已有的引用功能
  • (Pro) 利用 Zend_Db_Table::delete 代理到适配器方法,因此您可以提供一个 SQL 部分数组。来自文档:

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

  • (Con) 创建一个 Zend_Db_Select 对象,但您实际上只对 where 子句感兴趣

如果 delete 方法接受一个 Zend_Db_Select 或者可能是一个 Zend_Db_Constraint(不存在的类),那就更好了。您当然可以将其设为实用程序方法,以便您的所有表都可以使用它,从而使删除更容易。

另一种方法是简单地跳过选择对象:

$where = "{$this->getAdapter()->quoteInto('modID = ?', (int)$mid)} AND 
{$this->getAdapter()->quoteInto('URL = ?', $url->toString)}";

关于php - 从表中删除会导致违反基数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1388610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com