views:

46

answers:

1

I have this query:

SELECT
    groups.name
    categories.name,
    categories.label
FROM
    groups
JOIN
    categories
ON
    (categories.group1 = groups.id
OR
    categories.group2 = groups.id)
AND
    groups.label = :section
AND
    categories.active = 1

Now, this is my JOIN using Zend_Db_Select but it gives me array error

$select->from($dao, array('groups.name', 'categories.name', 'categories.label'))
       ->join(array('categories', 'categories.group1 = groups.id OR categories.group2 = groups.id'))
       ->where('groups.label = ?', $group)
       ->where('categories.active = 1');

My error:

Exception information:

Message: Select query cannot join with another table

Does anyone know what I did wrong?

UPDATE / SOLUTION:

I've found the solution thanx to Eran. I just post the solution here in case anyone else is stuck on a problem like this one. The solution is:

$db = Zend_Registry::get('db');
$dao = new Default_Model_Db_CategoryDao('db');
$select = $dao->select();

$select->setIntegrityCheck(false)
       ->from(array('c' => 'categories'), array('name', 'label'))
       ->join(array('g' => 'groups'), 'c.group1 = g.id OR c.group2 = g.id', 'g.label')
       ->where('g.label = ?', $group)
       ->where('c.active = 1');

return $dao->fetchAll($select);
+2  A: 

You are using a Zend_Db_Table_Select object. Those by default have integrity check enabled and cannot select data that is outside of their table.

You can turn it off by adding -> setIntegrityCheck(false) to the select object before querying with it.

You can read more about it in the manual under Select API -> Advanced usage

Eran Galperin
I added ->setIntegrityCheck(false) and now I get: Mysqli prepare error: Unknown column 'groups.name' in 'field list'
Enrico Pallazzo
Don't use the table name in the column array, it is taken from the original Zend_Db_Table object
Eran Galperin