Magento Zend Database Read Queries


As a platform Magento is built on the Zend Framework, therefore we can utilise the Zend Framework Database Class for reading from and writing to the Magento database. These actions would normally be used in a custom magento module or standalone script that you have running on your site.

Direct database actions should be handled with care as they do exactly as it says and make changes directly to the database… hmmm obviously.

So lets jump in with some examples (if you need to know how to make a stand alone script in Magento please click the link). All of the following snippets rely on $read being setup:

Some simple queries:

The code above will create an array of all of the products in the Magento database. Using the “fetchAll” function returns all of the results. There are other variations on this, these would be:

Normally fetchOne() would be used in conjunction with passing a extra variable inside the “from()” function withing the query to define which column you want to return, for example:

You can see that we pass a second parameter in the from() section of the query, this will define which row is returned.

Further information on what function can be added to your read queries can be found at: http://framework.zend.com/manual/1.12/en/zend.db.select.html

Unknown source


Leave a reply