可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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); } }
回答2:
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.