【问题标题】:Select Query with Multiple Rows in Join Table in collection in Magento在 Magento 的集合中选择连接表中的多行查询
【发布时间】:2018-04-15 23:14:52
【问题描述】:

如果我在主表中加入的表有 2 行和 1 个父 ID,我如何在 Magento 的 _preparecollection 中执行 Select 语句。

我现在有的桌子。

表1(主表)

表 2(sales_flat_invoice_comment)

我目前的准备收藏

$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join( array('a'=> mgmx_sales_flat_invoice_comment), 'a.parent_id = main_table.entity_id', array('a.comment'));
$this->setCollection($collection);
return parent::_prepareCollection();

这个查询,在回显中会是这样的

选择main_table.*、a.comment 发件人mgmx_sales_flat_invoice_grid 发件人main_table 内连接mgmx_sales_flat_invoice_comment AS a ON a.parent_id = main_table.entity_id

但如果此查询在表 2 中找到超过 1 行,则会返回错误。

我想要的是像下面这样的东西

与 |作为分隔符。

如何在 Magento 的 _prepareCollection 中实现这一点。

【问题讨论】:

    标签: php mysql magento join zend-framework


    【解决方案1】:

    您必须按entity_id 分组,然后使用group_concat 创建您的评论栏。您可以在 group by 中定义分隔符。

    连接的列有长度限制。因此,根据您的单个 cmets 的长度和 cmets 的数量,您可能无法在结果中得到所有这些。

    我希望这有助于解决您的问题。

    【讨论】:

    • 我已经在 Mysql 上做到了。先生,我的最后一个问题是如何将其转换为 zend 框架查询
    【解决方案2】:

    要在 zend 框架中加载 group_concat,您可以使用 Zend_Db_Expr 对象定义它

    类似

    $collection->getSelect()->join(
        array('a'=> new Zend_Db_Expr('GROUP_CONCAT(mgmx_sales_flat_invoice_comment)')), 
        'a.parent_id = main_table.entity_id', 
        array('a.comment')
    );
    

    当您需要在 zend 框架中执行自定义数据库函数时,了解这些很方便。

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 2014-09-23
      • 2023-03-20
      • 1970-01-01
      • 2021-09-23
      • 2012-03-17
      • 2017-07-23
      • 2018-04-13
      • 2012-06-11
      相关资源
      最近更新 更多