joinLeft Zend Framework, the same field names in different tables

Ertai picture Ertai · Nov 21, 2011 · Viewed 9.4k times · Source

I've got a problem. I'm trying to left join two tables with Zend Framework using $select object. Unfortunatly my tables has common field 'name' and when I'm joining one with the other the results I get is that name field from table overwrites the name field from the other.

My code is something like that:

$select->joinLeft ( array ('users' => 'users' ), $this->_name . '.employee_id = users.user_id', array ('*' ) );

How I can join tables and avoid this issue?

Answer

markus picture markus · Nov 21, 2011

Use table aliases as you would in any normal sql query!

With Zend_Db aliases are written like this:

$select = $db->select()
         ->from(array('p' => 'products'),
                array('product_id', 'product_name'))
         ->join(array('l' => 'line_items'),
                'p.product_id = l.product_id',
                array() ); // empty list of columns

The non-zend query would look like this:

SELECT p.product_id, p.product_name 
FROM products AS p 
JOIN line_items AS l ON p.product_id = l.product_id;