在 Zend Php 中连接模型中的表 [英] Joining tables within a model in Zend Php

查看:37
本文介绍了在 Zend Php 中连接模型中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 Zend_Table 的用法,并且可以使用 Zend 函数从与该类关联的表中获取数据.例如,我有一个视频表,在另一个表中,我有视频与其所属类别之间的关联.

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.

推荐答案

这是一个粗略的类.它直接使用 Zend db 适配器,因此 zend_db_table 对象并不真正意识到这种关系,但它可以工作.

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);
   }
}

这篇关于在 Zend Php 中连接模型中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆