Serializable Fields in Magento Collections
I can of course reload the object in my loop, doing something like
$object = $object->load($object->getId())
This is not a reload - this is a load. Items (model instances) in collections are not loaded. They merely have had an associative array of result row data applied to them. It's an important and slightly confusing difference between these collection-owned model instances and those which are self-load data through their resource model.
In your collection's _afterLoad()
, iterate over the _items
and unserialize()
the appropriate field:
protected function _afterLoad()
{
parent::_afterLoad();
foreach ($this->getItems() as $item) {
$item->setData('field',unserialize($item->getData('field')));
$item->setDataChanges(false);
//The above sets items as not dirty.
//Value will be serialized on save via resource model.
}
return $this;
}
Or more simply, in your resource model collection:-
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection { protected function _afterLoad() { parent::_afterLoad(); foreach ($this->getItems() as $item) { $this->getResource()->unserializeFields($item); $item->setDataChanges(false); } return $this; } }