我有这个查询:
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
现在,这是我使用 Zend_Db_Select 的 JOIN,但它给了我数组错误
$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');
我的错误:
Exception information:
Message: Select query cannot join with another table
有人知道我做错了什么吗?
更新/解决方案:
感谢 Eran,我已经找到了解决方案。我只是在这里发布解决方案,以防其他人遇到这样的问题。解决办法是:
$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);
最佳答案
您正在使用 Zend_Db_Table_Select 对象。默认情况下,它们启用了完整性检查,并且无法选择表之外的数据。
您可以通过在查询之前将 -> setIntegrityCheck(false) 添加到选择对象来关闭它。
您可以阅读更多相关信息in the manual在选择 API -> 高级用法下
关于php - Zend_Db_选择 : Working with JOIN's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4006975/