2013-05-14 5 views
1

데이터 행이 하나있는 컬렉션이 있습니다. 나는 다음과 같은 경우,Magento 컬렉션에서 데이터 가져 오기

$collection->getData(); 

그것은, 아래와 같이 나에게

array(1) { 
    [0] => array(3) { 
     ["id"] => string(1) "1" 
     ["field1"] => string(10) "Field 1 Data" 
     ["field2"] => string(10) "Field 2 Data" 
    } 
    } 

를 배열을 제공하지만 $collection->getField1()을 수행 할 때 그것은 정의되지 않은 방법을 말한다. 내가 아는 한 PHP 마술 게터가 이렇게 작동해야합니다. 그게 아니야?

foreach 구조없이이 값을 얻는 방법.

답변

8

매직 getter 및 setter 메서드는 Varien_Object을 상속받은 Magento 개체에만 적용됩니다. Models and Blocks의 사례에서. 컬렉션은 모델도 블록도 아닙니다. 컬렉션은 0 - N 모델 개체가 포함 된 foreach 수 개체입니다.

컬렉션의 getData 메서드는 컬렉션의 각 모델에 대한 원시 PHP 배열을 반환합니다.

#File: lib/Varien/Data/Collection/Db.php 
public function getData() 
{ 
    if ($this->_data === null) { 
     $this->_renderFilters() 
      ->_renderOrders() 
      ->_renderLimit(); 
     $this->_data = $this->_fetchAll($this->_select); 
     $this->_afterLoadData(); 
    } 
    return $this->_data; 
} 

아마도 컬렉션에서 첫 번째 모델을 가져 와서 데이터를 가져오고 싶을 것입니다.

$data = $collection->getFirstItem()->getData(); 
$field1 = $collection->getFirstItem()->getField1(); 
+0

$ data- $ collection-> getFirstItem() -> getData(); 내가 찾고 있던거야. 감사 –

관련 문제