Ver Mensaje Individual
  #4 (permalink)  
Antiguo 01/02/2011, 19:32
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: [problema] ZF y AJAX

Ok, primero refactoriza y que se envien correctamente las cabeceras y luego, vemos si el error persiste.

Para refactorizar:

ClientesMapper debe devolver el resultset

Código PHP:
Ver original
  1. public function search($p1, $p2)
  2. {
  3.    $select = $this->getDbTable()->select()
  4.     ->from(array('pac' => 'clientes'),
  5.     array('rut'=>'pac.rut', 'nombre'=>'CONCAT(pac.nombres, " " , pac.apellidoPaterno)'))
  6.     ->where($p1.' LIKE ?', '%'.$p2.'%')
  7.     ->order('apellidoPaterno ASC');
  8.                                    
  9.     return $this->getDbTable()->fetchAll($select);
  10. }

ClientesController inicializa los contextos y asigna el resultado del mapper a la vista.

Código PHP:
Ver original
  1. class ClientesController extends Zend_Controller_Action
  2. {
  3.     public function init()
  4.     {
  5.         $contextSwitch = $this->_helper->getHelper('contextSwitch');
  6.         $contextSwitch->addActionContext('search', 'xml')
  7.                       ->initContext();
  8.     }
  9.  
  10.     public function searchAction()
  11.     {              
  12.         $_request = $this->getRequest();
  13.        
  14.         if ($_request->isGet())
  15.         {
  16.             $filtro = $_request->getQuery('filtro');
  17.             $criterio = $_request->getQuery('criterio');
  18.            
  19.             if (null !== $filtro && null !== $criterio)
  20.             {
  21.                 //instancio el modelo para invocar al metodo
  22.                 $clientes= new Application_Model_Clientes();
  23.                 $this->view->clientes = $clientes->search($filtro, $criterio);
  24.             }
  25.         }
  26.     }  
  27. }

En la vista del contexto iterar y crear el xml, el fichero se debe llamar search.xml.phtml

Código HTML:
Ver original
  1. <?xml version='1.0' encoding='utf-8' ?>
  2. <items>
  3. <?php if (0 < count($this->clientes)) : ?>
  4. <?php foreach ($this->clientes as $row) : ?>
  5.      <RUT><?php echo $row->rut; ?></RUT>
  6.      <NOMBRES><?php echo $row->nombre; ?></NOMBRES>
  7. <?php endforeach; ?>
  8. <?php else : ?>
  9.     <RUT>1</RUT>
  10.     <NOMBRES>No se encontraron coincidencias</NOMBRES>
  11. <?php endif; ?>  
  12. </items>

Y por último la peticion ajax debe tener el parametro format con el contexto

Código Javascript:
Ver original
  1. ...
  2. function loadItems(tipo)
  3. {
  4.     var filtro;
  5.  
  6.     if (document.frm_buscar_clientes.filtros[0].checked){
  7.         filtro='apellidoPaterno';
  8.     } else {
  9.         filtro='rut';
  10.     }
  11.     getXML("/filtro/"+filtro+"/criterio/"+tipo+"/format/xml");
  12. }
  13. ...

Debería ser algo así, no lo eh probado así que puede tener typos, probalo y si falla algo comentalo.

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)