2012-02-07 89 views
1

我很困惑,为什么Zend_DB不接受WHERE子句的数组 - 或者我不正确?我做了以下解决方法:Zend DB fetchAll():其中数组

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

有些令人失望,我失去的东西吗?

+1

我会避免覆盖的结果阵列 – Phil 2012-02-07 01:06:45

回答

10

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

你'$ all'数据库表的对象,才能使用这个符号就不会$到哪里去是一个select()对象? $ where = $ this-> select(); $ where-> where(array('id> 0','enabled =?',1));或类似的东西? – RockyFord 2012-02-07 05:47:22

+1

@RockyFord当'fetchAll()'的'$ where'(第一个)参数是一个数组时,内部会发生这种情况 – Phil 2012-02-07 06:04:33

0

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

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