Replacement for Varien_data_collection
質問
What is the replacement for Varien_Data_Collection
in magento 2? I know that Varien_Object
has been replaced with \Magento\Framework\DataObject
.
解決
replacement for Varien_Data_Collection in magento 2?
You can find the data collection class in Magento 2 by your root\vendor\magento\framework\Data\Collection.php
The class Implements
\IteratorAggregate, \Countable, ArrayInterface, CollectionDataSourceInterface
Hope this helps.
他のヒント
The replacement of Varien_Data_Collection
is :
protected $_collectionFactory;
public function __construct(
\Magento\Framework\Data\CollectionFactory $collectionFactory
) {
$this->_collectionFactory = $collectionFactory;
}
public function methodName(){
$collection=$this->collectionFactory2->create();
//this line of code should written in `foreach() loop`
foreach($ArrayData as $RowData){
$varienObject = new \Magento\Framework\DataObject(); $varienObject->setData($RowData->getData());
$collection->addItem($varienObject);
// end `foreach() loop code`
}
}
Note: This code is Tested
You can have alternative like this:
protected $_collectionFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Data\CollectionFactory $collectionFactory,
array $data = []
)
{
$this->_collectionFactory = $collectionFactory;
parent::__construct($context, $data);
}
$collection=$this->_collectionFactory->create();
foreach($result as $row){
$varienObject = new \Magento\Framework\DataObject();
$varienObject->setData((array)$row);
$collection->addItem($varienObject);
}
所属していません magento.stackexchange