Joining Tables With Zend Framework PHP

怎甘沉沦 提交于 2020-01-05 13:28:09

问题


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.


回答1:


$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.




回答2:


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)


来源:https://stackoverflow.com/questions/1199383/joining-tables-with-zend-framework-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!