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.
Tables I have now.
Table 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
SELECT
main_table
.*,a
.comment
FROMmgmx_sales_flat_invoice_grid
ASmain_table
INNER JOINmgmx_sales_flat_invoice_comment
ASa
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.
What I want is for something like the one below
With | as a delimiter.
How Can I achieved this in the _prepareCollection of Magento.
2
Answers
you have to group by the
entity_id
and then usegroup_concat
to create your comment column. 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
Something like
Which is handy to know about whenever you need to do custom database functions inside the zend framework.