Subqueries no Zend Framework 2

Essa dica veio do amigo Romulo Busatto, de uma forma de criar subqueries usando o componente Zend\Db do Zend Framework 2. No controller vamos usar o Zend\Db\Sql para criar as consultas: <?php namespace Application\Controller; use Zend\View\Model\ViewModel; use Zend\Mvc\Controller\AbstractActionController; use Zend\Db\Sql\Sql; use Zend\Db\Sql\Expression; /** * Controlador exemplo * * @category Application * @package Controller * @author Elton Minetto <eminetto@coderockr.com> */ class IndexController extends AbstractActionController { /** * Exemplo de uso de sub queries * @return void */ public function indexAction() { //adapter configurado no ServiceManager $adapter = $this->getServiceLocator()->get('DbAdapter'); $sql = new Sql($adapter); $mainSelect = $sql->select()->from('comments'); $selectPost = $sql->select() ->from('posts') ->columns(array('title')) ->where('id = post_id'); $mainSelect->columns( array( 'description', 'name', 'comment_date', 'post_title' => new Expression('?...

March 21, 2013 ยท Elton Minetto