tags:

views:

211

answers:

2

Hi,

I am relatively new to the Zend Framework.

I understand the usage of Zend_Table and can obtain data using the Zend functions from the table associated with that class.

For example I have a video table and in another table I have the association between the video and what category it is in.

Im a little stumped how to active a select like the following within the framework:

SELECT * FROM video,category WHERE category.category_id = 3 AND video.id = category.video_id

I wish to do this within the video model which refers to the video table.

Thanks.

A: 

From what you have posted, it seems that there is a relationship between the tables for videos and categories: a category has many videos and a video belongs to a category. You should check out this article on Zend_Db_Table relationships.

Ignas R
+1  A: 

Here is a rough class. Its using the Zend db adapter directly so the zend_db_table object isn't really aware of the relationship, but it works.

class Video extends Zend_Db_Table
{
   public function doQueryWithSql($id)
   {
     $qy = " SELECT * FROM video,category WHERE category.category_id = $id AND video.id = category.video_id ";
     return $this->getAdapter()->fetchAll($qy);
   }

   public function doQueryWithObject($id)
   {
     $select = $this->getAdapter()->select();
     $select->from(array('v'=>'video'))
            ->join(array('c'=>'category'),'v.id = c.video_id')
            ->where("c.category_id = $id");
     return $this->getAdapter()->fetchAll($select);
   }
}
txyoji