开发者

Zend_Db_Select: Working with JOIN's

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 = n开发者_C百科ew 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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜