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(), ...

Get Magento 2 Developer's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.