Book Image

Magento 2 Development Cookbook

Book Image

Magento 2 Development Cookbook

Overview of this book

With the challenges of growing an online business, Magento 2 is an open source e-commerce platform with innumerable functionalities that gives you the freedom to make on-the-fly decisions. It allows you to customize multiple levels of security permissions and enhance the look and feel of your website, and thus gives you a personalized experience in promoting your business.
Table of Contents (18 chapters)
Magento 2 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with Magento collections


When you want to retrieve a set of entities of the same type, we usually use a query to get the data of a table.

But in Magento not every entity is stored in a single table and that means that a very complex query is required to get the data. A generic system/language to create queries for Magento entities was the solution.

For this solution, Magento has created a system called collections. A collection is a set of entities of the same type where you can add filters to it to customize your result.

In this recipe, we will see what we can do with Magento collections.

Getting ready

For this recipe, it is required to have the Packt_HelloWorld module installed with the code of the previous recipe Creating a flat table with models.

How to do it...

The next examples show the possibilities of working with Magento collections:

  1. Create a Collection controller action by creating the file app/code/Packt/HelloWorld/Controller/Index/Collection.php with the following content:

    &lt...