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

<?php
$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)

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

3). Fetching data:

<?php
 $allTags = $zendDb->fetchAll($query);
?>

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

That’s all.
Enjoy

by