Magento Quickies

All the Non-Trivial Magento Trivia

3 notes

Call to a member function getBackend() on a non-object

I needed to grab a few category names when all I had were the category IDs

$categories = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('category_id', array('in'=>$product['category_ids']));

Can you spot the error? PHP complained with the following error

Fatal error: Call to a member function getBackend() on a non-object in /Users/alanstorm/Sites2014/ on line 816

Can you spot the error now? Unless you happen to be working on a project where you’re deep inside Magento’s EAV implementation, the above error message is worse than useless when it comes to tracking down the actual error.

The problem was I used category_id — and a Magento category object has no such field. I should have used entity_id. Easy brain fart — made easier in that Magento doesn’t stick to the entity_id convention.

Why such a cryptic error message? Because somewhere along the line Magento’s EAV code checks if the filter attribute is or is not a static attribute.

#File: app/code/core/Mage/Eav/Model/Entity/Abstract.php
public function isAttributeStatic($attribute)
    $attrInstance       = $this->getAttribute($attribute);
    $attrBackendStatic  = $attrInstance->getBackend()->isStatic();
    return $attrInstance && $attrBackendStatic;

Magento tried to load a category_id attribute

$attrInstance = $this->getAttribute('category_id');

This failed because there’s no such attribute. Magento returned false. Then Magento tried to call a method on something it expected to be an object, but ended up being a boolean.


That’s why we got the cryptic error message. A certified or experienced developer will be able to track this sort of thing down — but to a newcomers cryptic error messages are kryptonite.

0 notes

Beware of 0 Values in Primary Keys

In a stock Magento system, there’s several tables where a row will have an ID field of 0.

mysql> select * from core_store\G:
*************************** 1. row ***************************
  store_id: 0
      code: admin
website_id: 0
  group_id: 0
      name: Admin
sort_order: 0
 is_active: 1
*************************** 2. row ***************************
  store_id: 1
      code: default
website_id: 1
  group_id: 1
      name: Default Store View
sort_order: 0
 is_active: 1

In turn, there are times where the Magento application will assume these 0 ID rows exist. For example, a store ID of 0 is always interpreted as the admin store object.

In day to day operations this is no big deal — however MySQL has a bit of default behavior that might lead to some weird problems. Specifically, if you use 0 in an INSERT for a primary key, MySQL interprets that as a request to generate a new key.

Again, day to day, there won’t be any problems running Magento with these rows, since the 0 ID already exists. However, if you need to move or upgrade the data you might run into a problem. Consider an OUTFILE export of the above table.

mysql> select * from core_store INTO OUTFILE '/tmp/test.sql';

$ cat /tmp/test.sql 
0   admin   0   0   Admin   0   1
1   default 1   1   Default Store View  0   1

The OUTFILE command correctly exports the ID of 0. However, if you perform an INFILE import, or create INSERTs manually from this data, the 0 will INSERT as a PRIMARY KEY request, meaning the admin store will no longer have an ID of 0, meaning Magento will break.

You can fix this during import by running the following


The sql_mode MySQL variable tells MySQL if it should interpret 0 INSERTs as a true 0, not a PRIMARY KEY generation request.

One sharp stick in the eye for anyone who says MySQL’s lax data integrity doesn’t cause real world tradeoffs.

0 notes

Using the Magento API to Safely Serialize as JSON

If you’re new to Magento, you may not realize that every remote API method has a native PHP equivalent. The why, how, and how to identify these models is covered elsewhere, but one thing I forget at my peril is how useful the API methods are when I need to serialize something as JSON or via PHP’s serialize function.


$data = serialize(Mage::getModel('catalog/product')->load($product_id));

What’s great about the API load methods is they automatically return a simple PHP array full of scaler values. Compare that to something like this


While they may seem equivalent, the catalog/product object is going to have a lot of other object references — which is turn may have other other object references, which in turn may circle back to the product object. This isn’t insurmountable, but is a giant pain in the butt when all you want is a JSON representation of a product’s data. The API models give you this, with the added benefits that they’re relatively stable — array keys added to an API result are much less likely to change version to version.

0 notes

Anti-SQL Bias

Developers coming into Magento are often put off by seemingly voodoo rituals like “Don’t directly manipulate the database tables with SQL”. This may be a ritual, but it’s a smart one, and my answer to this questions tells you why. It also includes an example SQL query for fetching all the product status values when you’re ready to dive deep on Magento’s EAV structure.

1 note


When I started using Magento the System Configuration section made me feel a little dumb — so many options, but no easy way to search for things. I’d always forget where a particular option was.

After a few months of using grep and ack to search though the system.xml files and work backwards to figure out the section, I decided to automate that process and the System Configuration Search module was the result.

Tim Reynolds had the same idea, and created a similar module named qconfig. I’ve also seen a few other implementations floating around, but I really like what Tim’s done with the search visualizations. It makes me extra embarrassed about my goofy dancing arrows. (although I still prefer my plain text results for discovery)

It’s a nice example of the way numerous individuals can have the same idea and pursue it independently of one another without realizing what the other people are working on.