2011-12-01 85 views
4

我定製的Magento FAQ extension的排序FAQ項目由category.below集合 用來獲取所有項目活動的常見問題的項目。Magento的連接表收集

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

有關係表 「faq_category_item

表結構: -

category_id faq_id 
    1    1 
    2    2 
    1    3 

所以我決定把兩個tables.I不成功在這。 我想下面是什麼。

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

哪些錯誤在此,我怎麼能過濾特定類別items.Please分享一些好的鏈接以瞭解Magento的模型集合。

在此先感謝

回答

8

getSelect()join()返回的類型是選擇對象,而不是集合addStoreFilter()addIsActiveFilter()屬於。選擇部分需要發生在鏈的後面:

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

:thanks.can你很好的解釋分享一些鏈接,瞭解法師模型集合。 – Gowri

+2

您可以用[艾倫風暴的教程](http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections)或[他的博客(http開頭://alanstorm.com/category/magento)。 – clockworkgeek

4

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') 
{ 

那麼試試這個功能的加入表,你可以這樣做:

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

我得到了joinTable函數的錯誤。我應該在任何地方聲明這個函數。 – Gowri

+1

呃... ...很奇怪。什麼輸出,如果嘗試這個片段你:print_r的(get_class_method(0函數get_class($集合))) –