O'Reilly logo

Magento 2 Developer's Guide by Branko Ajzele

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Managing collections

Let's start with EAV model collections. We can instantiate the collection either through the entity factory class like follows:

$collection = $this->employeeFactory->create()
                   ->getCollection();

Or we can use object manager to instantiate the collection as shown next:

$collection = $this->_objectManager->create(
    'Foggyline\Office\Model\ResourceModel\Employee\Collection's
);

There is also a third way, which might be the preferred one, but it requires us to define APIs so we will skip that one for the moment.

Once we instantiate the collection object, we can loop through it and do some variable dumps to see the content on individual $employee entities, like shown next:

foreach ($collection as $employee) { \Zend_Debug::dump($employee->toArray(), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required