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

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

$collection = $this->_objectManager->create(

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.