Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I create a collection that combines one table with catalog product data? 
 
ShopGuy
Guru
 
Total Posts:  462
Joined:  2008-09-07
 

Okay, I am trying to write a module where people can ask questions about products on the product page. So, I have my questions table and my answers table. I want to create a collection that combines the questions with the catalog product. What I mean by that is I want the collection to return product data (name, sku, etc) with the questions. So, I can do the following:

$questionCollection $questionModel->getCollection()
$questionCollection->joinProducts();
$questionCollection->addProductAttribute('name');

foreach (
$questionCollection as $item{
echo $item->getName();
}

Because of the EAV model I have no idea how to do this. I can do it in reverse (create a collection that consists of a catalog product with the question table joined to it), but that creates problems (getId() refers to entity id instead of question id) and is not very elegant.

Anyone know how to do this? There must be a way. Any help would be appreciated. I have spent 2 days on this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
wdchoi
Jr. Member
 
Total Posts:  20
Joined:  2008-11-17
 

Hi ShopGuy.

If I got it right, I believe there can be several questions for one product, and several answers for one question. This should be easy.

In your questions table, you should put a column called SKU or entity_id.

Then you should do this :

$questionCollection $questionModel->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('SKU''<your_product_SKU>');
or
$questionCollection $questionModel->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('product_id'$product->getId());

You then have your question collection for one product. Loop in it to echo it, and that should do it !

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top