zend-dbinner-joinzend-db-select

zend db join resultset


I used zend_db_select for joining 3 tables, and in the result set array while I was expecting to see the column names with aliases, it returns an array with keys having no aliases.

$dbSelect = $db->select()->from(array("pp"=>"products_photos"),array())
                         ->joinInner(array("ph"=>"photos"), "pp.photo_id=ph.photo_id","ph.photo_id")
                         ->joinInner(array('pr'=>'products'),"pr.product_id=pp.product_id","pr.product_id")
                         ->where("pr.product_id=$row->product_id");

$photoJoinRowSet = $db->fetchAll($dbSelect);
var_dump($photoJoinRowSet);die();

RESULT LIKE :

array(2) { [0]=> array(3) { ["product_id"]=> string(1) "1" ["photo_id"]=> string(1) "4" }}

While I was expecting :

array(2) { [0]=> array(3) { ["pr.product_id"]=> string(1) "1" ["ph.photo_id"]=> string(1) "4" }}

......i.e with column aliases.

Does anyone know why this happens?? thanks.


Solution

  • You didn't specify any aliases here, so your select will translate to something like SELECT ph.photo_id, pr.product_id, without AS, which will return photo_id and product_id as expected.

    You need to specify explicitly your aliases if you want the dots in the keys:

    $dbSelect = $db->select()->from(array("pp"=>"products_photos"),array())
        ->joinInner(array("ph"=>"photos"), "pp.photo_id=ph.photo_id",
          array("ph.photo_id" => "ph.photo_id"))
        ->joinInner(array('pr'=>'products'), "pr.product_id=pp.product_id",
          array("pr.product_id" => "pr.product_id"))
        ->where("pr.product_id=$row->product_id");
    

    More information on the Zend_Db_Select documentation.