简体   繁体   English

Zend_Db_Select:使用JOIN的

[英]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 现在,这是我使用Zend_Db_Select的联接,但是它给了我数组错误

$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. 我找到了Eran的解决方案thanx。 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);

You are using a Zend_Db_Table_Select object. 您正在使用Zend_Db_Table_Select对象。 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. 您可以通过在选择对象之前添加-> setIntegrityCheck(false)到选择对象来关闭它。

You can read more about it in the manual under Select API -> Advanced usage 您可以在手册中的 “选择API->高级用法”下阅读更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM