Zend Framework: Zend_Db_Select - how to join custo

2019-01-24 17:15发布

问题:

$select->joinRight(array('i' => '(SELECT * FROM images ORDER BY image_id)'),'i.ad_id = '. $main .'.id',$imarray);

Like that doesn't work. Subquery getting inside quotes.

Like that:

RIGHT JOIN `(SELECT * FROM images ORDER BY image_id)` AS `i` ON i.ad_id = a.id

Thanks ;)

回答1:

Use

$select->joinRight(
    array('i' => new Zend_Db_Expr('(SELECT * FROM images ORDER BY image_id)')),
    'i.ad_id = '. $main .'.id',
    $imarray
);


回答2:

I feel This is easier to read and navigate...

      $sub = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('i' => 'images'), array('*'))
            ->order('i.image_id');

$select = $this->select()
             ->setIntegrityCheck(false)
             ->from(array('m' => 'MAIN_TABLE'), array('*'))
             ->joinRight(array('i' => $sub), 'i.ad_id = m.id', array('*'));

   return $this->select($select);