简体   繁体   English

在Magento的集合中选择连接表中的多行查询

[英]Select Query with Multiple Rows in Join Table in collection in Magento

How Do I do Select Statement in _preparecollection in Magento if the table I joined in the Main table has 2 rows with 1 parent ID.如果我在主表中加入的表有 1 个父 ID 的 2 行,我如何在 Magento 的 _preparecollection 中执行 Select 语句。

Tables I have now.我现在有的表。

Table 1(Main Table)表1(主表) 在此处输入图片说明

Table 2(sales_flat_invoice_comment)表 2(sales_flat_invoice_comment) 在此处输入图片说明

My Current Prepare Collection我目前的准备收藏

$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();

This query, in echoed will be like this这个查询,在 echoed 中将是这样的

SELECT main_table .*, a . SELECT main_table .*, a . comment FROM mgmx_sales_flat_invoice_grid AS main_table INNER JOIN mgmx_sales_flat_invoice_comment AS a ON a.parent_id = main_table.entity_id comment来自mgmx_sales_flat_invoice_grid AS main_table INNER JOIN mgmx_sales_flat_invoice_comment AS a ON a.parent_id = main_table.entity_id

But it will return an error if this query finds more than 1 row in table 2.但如果此查询在表 2 中找到多于 1 行,它将返回错误。

What I want is for something like the one below我想要的是像下面这样的东西

在此处输入图片说明

With |与 | as a delimiter.作为分隔符。

How Can I achieved this in the _prepareCollection of Magento.我如何在 Magento 的 _prepareCollection 中实现这一点。

you have to group by the entity_id and then use group_concat to create your comment column.您必须按entity_id ,然后使用group_concat创建您的评论列。 You can define a separator in the group by.您可以在组中定义分隔符。

The concatenated column has a limit in length.连接列的长度有限制。 So depending on the length of your single comments and the number of comments it might happen that you do not get all of them in the result.因此,根据您的单个评论的长度和评论的数量,您可能无法在结果中获得所有评论。

I hope this helps with solving your problem.我希望这有助于解决您的问题。

To get the group_concat loading in the zend framework you can define it using the Zend_Db_Expr object要在 zend 框架中加载 group_concat,您可以使用 Zend_Db_Expr 对象定义它

Something like类似的东西

$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')
);

Which is handy to know about whenever you need to do custom database functions inside the zend framework.当您需要在 zend 框架内执行自定义数据库功能时,了解这一点很方便。

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

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