Search code examples
zend-frameworkzend-db-select

Zend DB Select version of a simple Mysql Left Join


I have a simple Left query on mysql

SELECT sp. * , p.name, p.date_created
FROM subpages sp
LEFT JOIN pages p ON p.id = sp.page_id

I dont know how to execuite this query from zend framework.

I have a Mapper Page, which access a DbTable page which is extending Zend_Db_Table_Abstract. I read some articles so i suppose statement should look something like this from the mapper

$select = $this -> DbTable() -> select();
$select -> joinleft(..... This is what I dont know how to write....);
$results = $this -> DbTable() -> fetchAll($select);
if($results) { return $result; }

In the article, $select -> from() were used, that where my mind is stuck , why do we need to write $select -> from("subpages") when It will already be defined in the DbTable page.

How to write the join statement properly?


Solution

  • If you want to use the select with multiple tables, you need to get it from the table adapter, which is a Zend_Db object (and you'll need to specify your table in the from() method).

    So, you would need something like the following:

    $select = $this->DbTable()->getAdapter()->select();
    $select->from(array("mi" => "tableName"));
    $select->joinLeft(array("p" => "pages"), "p.id = mi.page_id");
    (etc)
    

    Hope that helps,