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

이 약간 실망, 내가 뭔가를 놓친 거지 내가 주변에 다음과 같은 작업을 만들었습니다?

/** 
* 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) 

Zend_Db_Table_Abstract에서

+1

() 객체를 ? $ where = $ this-> select(); $ where-> where (array ('id> 0', 'enabled =?', 1)); 또는 비슷한 무엇입니까? – Phil

답변

10

그래서 당신은 fetchAll() 어디 조항의 배열을 받아 수행, 올바르지 않습니다.

귀하의 배열이

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

와'$의 all' DB 테이블 객체를 덮어 쓰지 않도록 것 – RockyFord

+1

@RockyFord 이것은'fetchAll()'의'$ where' (첫 번째) 인수가 배열 일 때 내부적으로 발생합니다 – Phil

0

먼저 우리는 원래의 코드를 보면됩니다 (Zend_Db_Select의 정의에 따라) 다음과 같아야합니다

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

할당 연산자입니다=> 기억 . 위의 배열에서 키 0에 자동으로 할당 된 문자열로 시작합니다. 다음 요소는 'enabled' 키에 할당 된 번호 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); 
나는 선택이 될이 표기법을하지 $ 할 경우 사용하기 위해 결과 배열