php - Zend_Db_选择 : Working with JOIN's

标签 php sql zend-framework select join

我有这个查询:

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/

相关文章:

PHP json_decode 跳过部分源 JSON

sql - 获取每个 Group By SQL 的最新记录

php - 我应该在哪里填充我的 Zend_Navigation 容器?

php - Zend 数据库适配器 - 未捕获的异常 - 堆栈跟踪显示用户名和密码

c# - GROUP BY、ORDER BY 和 LINQ 中的第一

php - Zend 数据库优先考虑Where 语句

php - foreach 循环打印多次的结果

php - Symfony2-表单错误(对于整个表单,不是单个字段)不显示

php - mysql server has gone away,序列化和锁等待错误

mysql - 通过主键查找外键?