簡體   English   中英

從Magento Collection獲取數據

[英]Getting data from a Magento Collection

我有一個集合,其中包含一行數據。 如果我這樣做,

$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()它會顯示Undefined Method。 據我所知,php magic getter應該像這樣工作。 不是嗎?

任何想法如何在沒有foreach構造的情況下獲得此值。

魔術getter和setter方法僅適用於從Varien_Object繼承的Magento對象。 在模型和塊的實踐中。 集合既不是模型也不是塊。 集合是包含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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM