magento连接表集合

Gow*_*wri 4 join magento magento-1.5

我按类别为sort faq项目定制Magento FAQ扩展.下面的集合用于获取所有项目的活动常见问题项目.

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

有关系表" faq_category_item "

表结构: -

category_id    faq_id
   1              1
   2              2
   1              3 
Run Code Online (Sandbox Code Playgroud)

所以我决定加入两张桌子.我没有成功.我尝试的是下面.

$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();
Run Code Online (Sandbox Code Playgroud)

这有什么不对,我怎么能过滤特定的类别项目.请分享一些很好的链接来学习Magento模型集合.

提前致谢

clo*_*eek 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');
Run Code Online (Sandbox Code Playgroud)

  • 您可以从[Alan Storm的教程](http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections)或[他的博客]开始(http: //alanstorm.com/category/magento). (2认同)