Home » Magento tutorial for developer » Magento tutorial for developer – Part 10 – Magento Varien Data Collections

Magento tutorial for developer – Part 10 – Magento Varien Data Collections

Today we will continue the series of Magento tutorial for developer with Part 10 – Magento Varien Data 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”. I’ve long held that PHP arrays are a huge reason for the platform’s popularity. 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.

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. We’re going to take a look at Magento Collections, starting from the bottom and working our way up. Setup a controller action where you can run arbitrary code, and let’s get started.

A Collection of Things

First, we’re going to create a few new Objects.

The Varien_Object class defines the object all Magento Models inherit from. This is a common pattern in object oriented systems, and ensures you’ll always have a way to easily add methods/functionally to every object in your system without having to edit every class file. Any Object that extends from Varien_Object has magic getter and setters that can be used to set data properties. Give this a try

If you don’t know what the property name you’re after is, you can pull out all the data as an array

The above will give you an array something like

Notice the property named “last_name”? If there’s an underscore separated property, you camel case it if you want to use the getter and setter magic.

In more recent versions of Magento, use can use Array style bracket to access properties

The ability to do these kinds of things is part of the power of PHP5, and the development style a certain class of people mean when they say “Object Oriented Programming”. So, now that we have some Objects, let’s add them to a Collection. Remember, a Collection is like an Array, but is defined by a PHP programmer.

The Varien_Data_Collection is the Collection that most Magento data Collections inherit from. Any method you can call on a Varien_Data_Collection you can call on Collections higher up the chain (We’ll see more of this later) What can we do with a Collection? For one, with can use foreach to iterate over it

There are also shortcuts for pulling out the first and last items

Want your Collection data as XML? There’s a method for that

Only want a particular field?

The team at Magento have even given us some rudimentary filtering capabilities.

Neat stuff.

Model Collections

So, this is an interesting exercise, but why do we care? We care because all of Magento’s built in data Collections inherit from this object. That means if you have, say, a product Collection you can do the same sort of things. Let’s take a look

Most Magento Model objects have a method named getCollection which will return a collection that, by default, is initialized to return every Object of that type in the system. A Quick Note: Magento’s Data Collection’s contain a lot of complicated logic that handles when to use an index or cache, as well as the logic for the EAV entity system. Successive method calls to the same Collection over its life can often result in unexpected behavior. Because of that, all the of the following examples are wrapped in a single method action. I’d recommend doing the same while you’re experimenting. Also, XDebug’s var_dump is a godsend when working with Magento Objects and Collections, as it will (usually) intelligently short circuit showing hugely recursive Objects, but still display a useful representation of the Object structure to you. The products Collection, as well as many other Magento Collections, also have the Varien_Data_Collection_Db class in their ancestor chain. This gives us a lot of useful methods. For example, if you want to see the select statement your Collection is using

The output of the above will be

Whoops! Since Magento is using the Zend database abstraction layer, your Select is also an Object. Let’s see that as a more useful string.

Sometimes this is going to result in a simple select

Other times, something a bit more complex

The discrepancy depends on which attributes you’re selecting, as well as the aforementioned indexing and cache. If you’ve been following along with the other articles, you know that many Magento models (including the Product Model) use an EAV system. By default, a EAV Collection will not include all of an Object’s attributes. You can add them all by using the addAttributeToSelect method

Or, you can add just one

or chain together several

Lazy Loading

One thing that will trip up PHP developers new to Magento’s ORM system is when Magento makes its database calls. When you’re writing literal SQL, or even when you’re using a basic ORM system, SQL calls are often made immediately when instantiating an Object.

Magento doesn’t work that way. Instead, the concept of Lazy Loading is used. In simplified terms, Lazy loading means that no SQL calls are made until the client-programmer needs to access the data. That means when you do something something like this

Magento actually hasn’t gone out to the database yet. You can safely add attributes later

and not have to worry that Magento is making a database query each time a new attribute is added. The database query will not be made until you attempt to access an item in the Collection. In general, try not to worry too much about the implementation details in your day to day work. It’s good to know that there’s s SQL backend and Magento is doing SQLy thing, but when your’e coding up a feature try to forget about it, and just treat the objects as block boxes that do what you need.

Filtering Database Collections

The most important method on a database Collection is addFieldToFilter. This adds your WHERE clauses. Consider this bit of code, run against the sample data database (substitute your own SKU is you’re using a different set of product data)

The first parameter of addFieldToFilter is the attribute you wish to filter by. The second is the value you’re looking for. Here’s we’re adding a sku filter for the value n2610. The second parameter can also be used to specify the type of filtering you want to do. This is where things get a little complicated, and worth going into with a little more depth. So by default, the following

is (essentially) equivalent to

Take a look for yourself. Running the following

will yield

Keep in mind, this can get complicated fast if you’re using an EAV attribute. Add an attribute

and the query gets gnarly.

Not to belabor the point, but try not to think too much about the SQL if you’re on deadline.

Other Comparison Operators

I’m sure you’re wondering “what if I want something other than an equals by query”? Not equal, greater than, less than, etc. The addFieldToFilter method’s second parameter has you covered there as well. It supports an alternate syntax where, instead of passing in a string, you pass in a single element Array. The key of this array is the type of comparison you want to make. The value associated with that key is the value you want to filter by. Let’s redo the above filter, but with this explicit syntax

Calling out our filter

As you can see, the second parameter is a PHP Array. Its key is eq, which stands for equals. The value for this key is n2610, which is the value we’re filtering on. Magento has a number of these english language like filters that will bring a tear of remembrance (and perhaps pain) to any old perl developers in the audience. Listed below are all the filters, along with an example of their SQL equivalents.

Most of these are self explanatory, but a few deserve a special callout

in, nin, find_in_set

The in and nin conditionals allow you to pass in an Array of values. That is, the value portion of your filter array is itself allowed to be an array.

notnull, null

The keyword NULL is special in most flavors of SQL. It typically won’t play nice with the standard equality (=) operator. Specifying notnull or null as your filter type will get you the correct syntax for a NULL comparison while ignoring whatever value you pass in

from – to filter

This is another special format that breaks the standard rule. Instead of a single element array, you specify a two element array. One element has the key from, the other element has the key to. As the keys indicated, this filter allows you to construct a from/to range without having to worry about greater than and less than symbols

The above yields

AND or OR, or is that OR and AND?

Finally, we come to the boolean operators. It’s the rare moment where we’re only filtering by one attribute. Fortunately, Magento’s Collections have us covered. You can chain together multiple calls to addFieldToFilter to get a number of “AND” queries.

By chaining together multiple calls as above, we’ll produce a where clause that looks something like the the following

To those of you that just raised your hand, yes, the above example would always return 0 records. No sku can begin with BOTH an a and a b. What we probably want here is an OR query. This brings us to another confusing aspect of addFieldToFilter’s second parameter. If you want to build an OR query, you need to pass an Array of filter Arrays in as the second parameter. I find it’s best to assign your individual filter Arrays to variables

and then assign an array of all my filter variables

In the interest of being explicit, here’s the aforementioned Array of filter Arrays.

This will gives us a WHERE clause that looks something like the following

Wrap Up

You’re now a Magento developer walking around with some serious firepower. Without having to write a single line of SQL you now know how to query Magento for any Model your store or application might need.

About Alan Storm

avatar
Alan Storm is a technology strategist, software developer and writer living and working in Portland, OR. He publishes articles on his website http://alanstorm.com and is the creator of the Magento development extension Commerce Bug.

One comment

  1. avatar

    Danke habe schon länger nach gesucht.

    Gut erläuter Danke

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top