views:

380

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

Any advice would be great.

Thanks.

+2  A: 
$db->select()->from('video')->joinInner('category','video.id = category.video_id')->where('category.category_id = ?',3)

BTW: It looks like you have wrong db design. You should have category_id in your video table (if 1 video -> 1 category) or have a connection table (M:N), but it seems wrong to have video id stored in category.

Tomáš Fejfar
just a short fyi.$sql = $this->getDbTable()->select()->where('group_id = ?', $group_id)->joinInner('category','video.id = category.video_id')->where('category.category_id = ?',3);$result = $this->getDbTable()->fetchRow($sql);
wenbert
Yes, thanks for valuable addition ;)
Tomáš Fejfar
+1  A: 

I would use Zend_Db_Select:

    $select = $this->db->select()->from(array('v' => 'video'))
                   ->join(array('c' => 'category'),'v.id = c.video_id')
                   ->where('c.category_id = ?', 3);
    print_r($select->__toString());

Output:

SELECT `v`.*, `c`.* FROM `video` AS `v` INNER JOIN `category` AS `c` ON v.id = c.video_id WHERE (c.category_id = 3)
karim79
how would you read columns from the category table in this example. is it expected that the Zend_Db_Table_Abstract would define the referenceMap or can i pass an array of specific category column names to read?
emeraldjava