Joining tables within a model in Zend Php

2019-07-08 11:57发布

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.

2条回答
Animai°情兽
2楼-- · 2019-07-08 12:49

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.

查看更多
小情绪 Triste *
3楼-- · 2019-07-08 12:50

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);
   }
}
查看更多
登录 后发表回答