Joining tables within a model in Zend Php

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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.



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