Ver Mensaje Individual
  #4 (permalink)  
Antiguo 06/12/2011, 09:11
Avatar de ApipeMc
ApipeMc
 
Fecha de Ingreso: septiembre-2010
Ubicación: Medellín, Antioquia, Colombia
Mensajes: 76
Antigüedad: 13 años, 7 meses
Puntos: 1
Respuesta: hacer una subcosulta con Zend_Db_Expr

Eh solucionado mi problema de la siguiente manera

Código PHP:
Ver original
  1. class Usuario_Model_DbTable_Mensaje extends Zend_Db_Table_Abstract
  2. {
  3.  
  4.     protected $_name    = 'mensaje';
  5.     protected $_primary = 'id';
  6.    
  7.     public function getlistmensaje($id){
  8.        
  9.         $select      = $this->getAdapter()
  10.                             ->select()
  11.                             ->from('mensaje',array('max_time'=>new Zend_Db_Expr('MAX(fecha)')))
  12.                             ->group('de');
  13.         $subconsulta = $select->__toString();
  14.                    
  15.         return $this->getAdapter()
  16.                     ->select()
  17.                     ->from(array('m'   => 'mensaje'))
  18.                     ->join(array('u'   => 'usuario'), 'm.de = u.id', array('nombre'=>'nombre', 'apellido'=>'apellido'))
  19.                     ->join(array('tmp' => new Zend_Db_Expr('('.$subconsulta.')')), 'm.fecha = tmp.max_time', false)
  20.                     ->where('u.rol  = ?', 0)                   
  21.                     ->where('m.para = ?', (int)$id)
  22.                     ->order('tmp.max_time DESC')
  23.                     ->query()
  24.                     ->fetchAll();
  25.     }
  26.    
  27. }

Y este es resultado que me devuelve
Código MySQL:
Ver original
  1. SELECT `m`.*, `u`.`nombre`, `u`.`apellido`
  2. FROM `mensaje` AS `m`
  3. INNER JOIN `usuario` AS `u` ON m.de = u.id
  4. INNER JOIN (SELECT MAX(fecha) AS `max_time` FROM `mensaje` GROUP BY `de`) AS `tmp`
  5. ON m.fecha = tmp.max_time
  6. WHERE (u.rol = 0)
  7. AND (m.para = 20)
  8. ORDER BY `tmp`.`max_time` DESC

Gracias por su colaboración :)