Home    Articles


How to use Zend Db in magento’s frontend


In order to make some query’s into phtml files from your theme you need to do the following:

1). Instantiate the Zend DB class

$zendDb = Mage::getModel('core/resource')->getConnection('core_read');

2). Makeing the query: (let’s assume that you want to get all tags for a product directly from database)

$query = $zendDb->select()
						"tag.tag_id = tag_relation.tag_id"
	 		->where('tag_relation.product_id =? ', $_product->getId() );

3). Fetching data:

 $allTags = $zendDb->fetchAll($query);

Now that we have the tags for particular product stored in array, use foreach to display them.

That’s all.