2

I am new to magento . I am directly learning Magento 2 .Here I created a Collection class inside Model for a custom module as I got a tutorial on internet.

What is this collection ? How does it work? Can anyone explain me this in detail.

1

I'm going to quote the Magento documentation to help you understand the concept of collections:

Originally, as a PHP programmer, if you wanted to collect together a group of related variables you had one choice, the venerable Array. While it shares a name with C's array of memory addresses, a PHP array is a general purpose dictionary like object combined with the behaviors of a numerically indexed mutable array.

In other languages the choice isn't so simple. You have multiple data structures to chose from, each offering particular advantages in storage, speed and semantics. The PHP philosophy was to remove this choice from the client programmer and give them one useful data structure that was "good enough".

All of this is galling to a certain type of software developer, and PHP 5 set out to change the status quo by offering built-in classes and interfaces that allow you to create your own data structures.

$array = new ArrayObject(); 
class MyCollection extends ArrayObject{...} 
$collection = new MyCollection(); 
$collection[] = 'bar';

While this is still galling to a certain type of software developer, as you don't have access to low level implementation details, you do have the ability to create array-like Objects with methods that encapsulate specific functionality. You can also setup rules to offer a level of type safety by only allowing certain kinds of Objects into your Collection.

It should come as no surprise that Magento offers you a number of these Collections. In fact, every Model object that follows the Magento interfaces gets a Collection type for free. Understanding how these Collections work is a key part to being an effective Magento programmer.

On top of that I suggest you read Alan Storm's article about CRUD in M2

  • But I didn't find any code in Collections file inside my custom module to fetch all data.Then how data is fetched? – user2431224 Jul 6 '16 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.