Zend DB fetchAll():作为数组

发布于 2025-01-03 06:03:42 字数 450 浏览 3 评论 0原文

我很困惑为什么 Zend_DB 不接受 WHERE 子句数组 - 或者我错了?我做了以下工作:

$all = new ORM_Model_DbTable_Asset();
$wheres = array('id > 0', 'enabled' => 1);
$all = $all->fetchAll(implode(' AND ', $wheres))->toArray();

为了我所希望的:

$all = new ORM_Model_DbTable_Asset();
$wheres = array('id > 0', 'enabled' => 1);
$all = $all->fetchAll($wheres)->toArray();

有点令人失望,我错过了什么吗?

I'm confused as to why Zend_DB doesn't accept an array of WHERE clauses - or am I incorrect? I have made the following work around:

$all = new ORM_Model_DbTable_Asset();
$wheres = array('id > 0', 'enabled' => 1);
$all = $all->fetchAll(implode(' AND ', $wheres))->toArray();

for what I hoped would be:

$all = new ORM_Model_DbTable_Asset();
$wheres = array('id > 0', 'enabled' => 1);
$all = $all->fetchAll($wheres)->toArray();

Slightly disappointing, am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2025-01-10 06:03:42

来自 Zend_Db_Table_Abstract

/**
 * Fetches all rows.
 *
 * Honors the Zend_Db_Adapter fetch mode.
 *
 * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
 * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
 * @param int                               $count  OPTIONAL An SQL LIMIT count.
 * @param int                               $offset OPTIONAL An SQL LIMIT offset.
 * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
 */
public function fetchAll($where = null, $order = null, $count = null, $offset = null)

所以你错了,fetchAll() 确实接受一个 where 子句数组。

您的数组应如下所示(基于 Zend_Db_Select 中的定义

$where = array(
    'id > 0',
    'enabled = ?' => 1
);

From Zend_Db_Table_Abstract

/**
 * Fetches all rows.
 *
 * Honors the Zend_Db_Adapter fetch mode.
 *
 * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
 * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
 * @param int                               $count  OPTIONAL An SQL LIMIT count.
 * @param int                               $offset OPTIONAL An SQL LIMIT offset.
 * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
 */
public function fetchAll($where = null, $order = null, $count = null, $offset = null)

So you are incorrect, fetchAll() does accept an array of where clauses.

Your array should look like this (based on the definition in Zend_Db_Select)

$where = array(
    'id > 0',
    'enabled = ?' => 1
);
蓝眸 2025-01-10 06:03:42

首先,我们将查看您的原始代码:

$wheres = array('id > 0', 'enabled' => 1);

记住 => 是一个赋值运算符。在上面的数组中,您从自动分配给键 0 的字符串开始。下一个元素是分配给键'enabled' 的数字1。答案 1 中提出的解决方案将数字 1 分配给键 'enabled = ?'

试试这个:

$all = new ORM_Model_DbTable_Asset();
$where = array();
$where[] = 'id > 0';
$where[] = $all->quote_into('enabled >= ?', 1, 'INTEGER');  // 1 could be a variable
$result = $all->fetchAll($where);

First we will look at your original code:

$wheres = array('id > 0', 'enabled' => 1);

Remember that => is an assignment operator. In your array above you start out with string automatically assigned to key 0. The next element is the number 1 assigned to the key 'enabled'. The solution proposed in answer 1 assigns the number 1 to key 'enabled = ?'.

Try this:

$all = new ORM_Model_DbTable_Asset();
$where = array();
$where[] = 'id > 0';
$where[] = $all->quote_into('enabled >= ?', 1, 'INTEGER');  // 1 could be a variable
$result = $all->fetchAll($where);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文