简体   繁体   English

Zend Framework查询问题

[英]Zend Framework query issue

Look at this code: 看下面的代码:

$sql = $this->getAdapter()->select()
        ->from(array('mab' => 'module_adv_banner'))
        ->joinLeft(array('mafb' => 'module_adv_filebanner'),'mab.id = mafb.id_banner',array('source','type'))
        ->joinLeft(array('macb' => 'module_adv_contentbanner'),'mab.id = macb.id_banner',array('content','type'))
        ->where('mab.id = ?', $this->_id)
        ->order('position asc');

In this query I use 3 tables. 在此查询中,我使用3个表。 First with 'mab' alias - is the main banners table. 首先带有'mab'别名-是主横幅表格。 Two another tables are 'mafb' and 'macb' are specific tables for different banner types. 另外两个表是'mafb''macb'它们是不同横幅类型的特定表。 In case the banner is image or swf, it is stored in 'mafb' table, in case of banner-informer, I use another table - 'macb' 如果横幅是图片或SWF,则将其存储在“ mafb”表中,如果使用横幅信息,则使用另一个表- 'macb'

In this query I select all required elements of banner. 在此查询中,我选择标题的所有必需元素。 Only one of two specific tables have row with id_banner = mab.id = $this->_id, and each of this tables have field 'type'. 两个特定表中只有一个具有id_banner = mab.id = $this->_id,并且每个表都有字段“ type”。 If it finds this row in first table, value of 'type' is null, because it is overwritten by second table 'type' value. 如果在第一张表中找到该行,则'type'的值为null,因为它被第二张表的'type'值覆盖。 How can I solve that problem? 我该如何解决这个问题?

Best Regards, Ahmed. 最好的问候,艾哈迈德。

ps sorry if my explanation is difficult, I do not have good english knowledge. ps很抱歉,如果我的解释困难,我的英语水平不好。

Not going into the details of why you keep 2 tables for one thing (banners)... try prepending the tablename to the fields being selected: 不讨论为什么要为一件事情保留2个表(横幅)的详细信息...尝试将表名放在所选字段的前面:

  $sql = $this->getAdapter()->select()
        ->from(array('mab' => 'module_adv_banner'))
        ->joinLeft(array('mafb' => 'module_adv_filebanner'),'mab.id = mafb.id_banner',array('mafb.source','mafb.type'))
        ->joinLeft(array('macb' => 'module_adv_contentbanner'),'mab.id = macb.id_banner',array('macb.content','macb.type'))
        ->where('mab.id = ?', $this->_id)
        ->order('position asc');

That will give you 4 fields instead of 2 which you can use in PHP and sort out the thing you need. 这将为您提供4个字段,而不是您可以在PHP中使用的2个字段,并整理出所需的内容。 Although it's a bit strange, that the adapter didn't namespace colliding field names. 尽管有点奇怪,但是适配器没有名称空间冲突字段名称。

BTW, it is sometimes more convenient to pass array() as the 3rd argument to join() and use columns() to define the fields you actually want to pull out. 顺便说一句,有时将array()作为第三个参数传递给join()并使用columns()来定义您实际要拉出的字段有时更方便。

Use $key => $value notation to create aliases: 使用$key => $value表示法创建别名:

->joinLeft(array('mafb' => 'module_adv_filebanner'),'mab.id = mafb.id_banner',array('mafb.source', 'mafb_type' => 'type'))
->joinLeft(array('macb' => 'module_adv_contentbanner'),'mab.id = macb.id_banner',array('macb.content','macb_type' => 'type'))

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

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