从Zend中的2个表中获取结果

时间:2022-10-25 22:46:19

I have 2 database tables,

我有2个数据库表,

1. gallery -> id, title, description
2. gallery_images -> image_id, gallery_id, name, image_title, thumb_name, image_name

I am able to select the images for specific gallery_id, using my Admin_Model_GalleryImages and my result array looks like the following -

我可以使用我的Admin_Model_GalleryImages选择特定gallery_id的图像,我的结果数组如下所示 -

Array ( 
[0] => Array ( 
          [image_id] => 1 
          [gallery_id] => 24 
          [image_title] => NICEIC.png 
          [thumb_name] => thumb_.6386527349.png 
          [image_name] => 6386527349.png ) 

[1] => Array ( 
          [image_id] => 2 
          [gallery_id] => 24 
          [image_title] => gas_safe_logo_monoblack.png 
          [thumb_name] => thumb_2100528832.png 
          [image_name] => 2100528832.png )

Code in the Gallery Model :

图库模型中的代码:

require_once 'Zend/Db/Table/Abstract.php';
require_once APPLICATION_PATH . '/modules/admin/models/Gallery.php';

class Admin_Model_GalleryImages extends Zend_Db_Table_Abstract {

    protected $_name = 'gallery_images';
    protected $_referenceMap = array(
            'Gallery' => array(
            'columns'       => array('gallery_id'),
            'refTableClass' => 'Admin_Model_Gallery',
            'refColumns'    => array('id'),
            'onDelete'      => self::CASCADE,
            'onUpdate'      =>self::RESTRICT
        )
    );

    public function getImages($id){
        $galleryImages = new self();
        $galleryRowset = $galleryImages->select();
        $galleryRowset->where('gallery_id='.$id);
        $images = $galleryImages->fetchAll($galleryRowset);
        return $images;

listimages action in controller :

控制器中的listimages动作:

public function listimagesAction(){
        $id = $this->_getParam('id');


        $currentImages = Admin_Model_GalleryImages::getImages($id);
        if ($currentImages->count() > 0) {
            $this->view->galleryImages = $currentImages;
        } else {
            $this->view->galleriesImages = null;
        }
    }

But I want results from the 1st table along with the results from the 2nd table, as follows :

但我想要第1个表的结果以及第2个表的结果,如下所示:

Array ( 
    [0] => Array ( 
              [image_id] => 1 
              [gallery_id] => 24 
              [image_title] => NICEIC.png 
              [thumb_name] => thumb_.6386527349.png 
              [image_name] => 6386527349.png
              [id] => 24 
              [title] => Somename
              [description] => description )

I tried using findDependentRowset but couldn't get it working. Please let me know how I could achieve this. Any help is much appreciated.

我尝试使用findDependentRowset但无法使其正常工作。请让我知道如何实现这一目标。任何帮助深表感谢。

1 个解决方案

#1


1  

you can use join clause to select data from 2 tables

您可以使用join子句从2个表中选择数据

here is sudo code you can change it to your table name

这里是sudo代码,你可以将它改成你的表名

$query = $this->select();
$query->setIntegrityCheck(false);
        $query->from(array('g' => 'games'), array());
        $query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('g.title', 'g.asin', 'g.platform_id', 'r.rank'));
        $resultRows = $this->fetchAll($query);
        return $resultRows;

just change your column name with gallery and galleryimages

只需使用图库和galleryimages更改列名称即可

#1


1  

you can use join clause to select data from 2 tables

您可以使用join子句从2个表中选择数据

here is sudo code you can change it to your table name

这里是sudo代码,你可以将它改成你的表名

$query = $this->select();
$query->setIntegrityCheck(false);
        $query->from(array('g' => 'games'), array());
        $query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('g.title', 'g.asin', 'g.platform_id', 'r.rank'));
        $resultRows = $this->fetchAll($query);
        return $resultRows;

just change your column name with gallery and galleryimages

只需使用图库和galleryimages更改列名称即可