Magento Collection Collection

I am setting up Magento FAQs to sort faq items into categories. The following collection is used to get all the active faq elements.

$collection = Mage :: getModel('flagbit_faq/faq')->getCollection() ->addStoreFilter(Mage :: app()->getStore()) ->addIsActiveFilter(); 

relationship table exists faq_category_item "

Table structure: -

 category_id faq_id 1 1 2 2 1 3 

So, I decided to join the two tables. In this I do not agree. What I tried below.

 $tbl_faq_item = Mage::getSingleton('core/resource')->getTableName('faq_category_item'); $collection = Mage :: getModel('flagbit_faq/faq')->getCollection() ->getSelect() ->join(array('t2' => $tbl_faq_item),'main_table.faq_id = t2.faq_id','t2.category_id') ->addStoreFilter(Mage :: app()->getStore()) ->addIsActiveFilter(); 

What is wrong with this and how can I filter certain category items. Please share good links to explore the collections of Magento models.

Thank you in advance

+4
source share
2 answers

The return type from getSelect() and join() is the select object, not the collection to which addStoreFilter() and addIsActiveFilter() . Part of the selection should appear later in the chain:

 $collection = Mage :: getModel('flagbit_faq/faq')->getCollection() ->addStoreFilter(Mage :: app()->getStore()) ->addIsActiveFilter(); // Cannot append getSelect right here because $collection will not be a collection $collection->getSelect() ->join(array('t2' => $tbl_faq_item),'main_table.faq_id = t2.faq_id','t2.category_id'); 
+8
source

Try this feature from

 Mage_Eav_Model_Entity_Collection_Abstract /** * Join a table * * @param string|array $table * @param string $bind * @param string|array $fields * @param null|array $cond * @param string $joinType * @return Mage_Eav_Model_Entity_Collection_Abstract */ public function joinTable($table, $bind, $fields = null, $cond = null, $joinType = 'inner') { 

So, to join the tables, you can do the following:

 $collection->joinTable('table-to-join','left.id=right.id',array('alias'=>'field'),'some condition or null', joinType(left right inner)); 
+4
source

Source: https://habr.com/ru/post/1384049/


All Articles