Question

How can I do a Query like this using the Zend framework

SELECT * FROM `productos` AS `p` 
LEFT JOIN (SELECT SUM(cantidad) AS transferencias FROM transferencias WHERE upc = p`.`upc` and sucursal = 10) 
AS `trans` ON trans.upc = p.upc AND trans.sucursal_clave_destino = 10 

Thank you in advance.

Was it helpful?

Solution

You need to try this one. I can't try it but the way of resolve it you can use from my query

$this->getAdapter()
            ->select()
            ->from(array('p' => 'productos'))
            ->joinLeft(array('trans' => $this->getAdapter()
                                             ->select()
                                             ->from('transferencias', 'SUM(cantidad)')
                                             ->where('upc IN (?)', $this->getAdapter()
                                                                     ->select()
                                                                     ->from('productos', 'upc')
                                             )->where('sucursal = ?', 10)
            ), 'trans.upc = p.upc')
            ->where('trans.sucursal_clave_destino = ?', 10)
            ->query()
            ->fetchAll();

OTHER TIPS

First of all, I'm afraid it's impossible for your query to run because the syntax is wrong. The correct way to write it is:

SELECT *, SUM(trans.cantidad) as cantidad
FROM productos AS p
LEFT JOIN transferencias AS trans
ON p.upc = trans.upc
WHERE trans.sucursal_clave_destino = 10 AND trans.sucursal = 10

First approach

I assume you have created your model in this manner: http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html

For example, in your Default_Model_ProductosMapper:

function getXXXX(){

    $select = "[THE ABOVE QUERY]";
    $result = $this->getDbTable()->getAdapter()->fetchAll($select);
    return $result;
}

This is the most basic approach.

Second approach

By using Zend_Db functions, which is like prepare statement in database concept. Only use it to increase safety if you are passing parameters from user input (see SQL injection), otherwise it's safe to use the first approach.

Still, in your mapper:

function getXXXX(){

    $query = $this->getDbTable()->getAdapter()->select();
    $query->from('productos', array());
    $query->joinLeft('transferencias', 'productos.upc = transferencias.upc', array('SUM(trans.cantidad) as cantidad));
    $query->where("trans.sucursal_clave_destino = 10");
    $query->where("trans.sucursal = 10");

    // get result
    $stmt = $this->getDbTable()->getAdapter()->query($query);
    $result = $stmt->fetchAll();
    return $result;
}

Third approach

If you are using Database ORM like Doctrine2, you can also write SQL or DQL (Doctrine Query Language) queries, which syntax is highly similar with SQL queries but absolutely NOT the same mechanism. The document is here. This document covers both approaches above for DQL and also will tell you where to put them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top