2012-02-07 86 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);