[英]Getting data from a Magento Collection
I have a collection that has one row of data in it. 我有一个集合,其中包含一行数据。 If I do following,
如果我这样做,
$collection->getData();
it give me an array like below, 它给我一个像下面的数组,
array(1) {
[0] => array(3) {
["id"] => string(1) "1"
["field1"] => string(10) "Field 1 Data"
["field2"] => string(10) "Field 2 Data"
}
}
But when I do $collection->getField1()
it says Undefined Method. 但是当我执行
$collection->getField1()
它会显示Undefined Method。 As far as I know php magic getter should work like this. 据我所知,php magic getter应该像这样工作。 Isnt it?
不是吗?
Any Ideas how to get this value without a foreach construct. 任何想法如何在没有foreach构造的情况下获得此值。
The magic getter and setter methods only apply to Magento objects that inherit from Varien_Object
. 魔术getter和setter方法仅适用于从
Varien_Object
继承的Magento对象。 In practices that's Models, and Blocks. 在模型和块的实践中。 A collection is neither a model or a block.
集合既不是模型也不是块。 A collection is a
foreach
able object that contains 0 - N model objects. 集合是包含0-N模型对象的
foreach
对象。
A collection's getData
method will return the raw PHP array of each model in the collection. 集合的
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;
}
What you probably want to do is grab the first model from the collection, and then grab its data. 您可能想要做的是从集合中获取第一个模型,然后获取其数据。
$data = $collection->getFirstItem()->getData();
$field1 = $collection->getFirstItem()->getField1();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.