magento在grid.php prepareCollection中使用join
有人能告诉我如何在magento中加入吗 问题是:magento在grid.php prepareCollection中使用join,join,magento,Join,Magento,有人能告诉我如何在magento中加入吗 问题是: <?//kleurtjes $collection= Mage::getModel('faq/faq')->getCollection(); $collection->getSelect()->join(array('faqcat' => $this->getTable('faqcat/faqcat')), 'faqcat.faqcat_id=faq.faqcat_id' , array('faqcat.*
<?//kleurtjes
$collection= Mage::getModel('faq/faq')->getCollection();
$collection->getSelect()->join(array('faqcat' => $this->getTable('faqcat/faqcat')), 'faqcat.faqcat_id=faq.faqcat_id' , array('faqcat.*'));
?>
我正在尝试与表faqcat建立连接,在表中我使用键faqcat\u id
此外,我希望选择faqcat.name+faq.faq\u id,因为这些是我希望在列中使用的值
<?
protected function _prepareColumns()
{
$this->addColumn('faq_id', array(
'header' => Mage::helper('faq')->__('ID'),
'align' =>'right',
'width' => '50px',
'index' => 'faq_id',
));
$this->addColumn('name', array(
'header' => Mage::helper('faqcat')->__('Titel'),
'align' =>'left',
'index' => 'name',
));
}
?>
在尝试了1000种组合后,我不知道该怎么做了。。。谁愿意帮助我
这是完整的功能:
<?
protected function _prepareCollection()
{
$collection= Mage::getModel('faq/faq')->getCollection();
//$collection->getSelect()->join(array('faqcat' => $this->getTable('faqcat/faqcat')), 'faqcat.faqcat_id=faq.faqcat_id' , array('faqcat.*'));
$id = Mage::getModel('customer/session')->getCustomer()->getId();
$this->setCollection($collection);
// }
return parent::_prepareCollection();
}
?>
我想说清楚的是,这是我想要的sql,然后是magento方式
<?//kleurtjes
SELECT faq.faq_id as id, faqcat_name as name
FROM faq
JOIN faqcat
USING ('faqcat_id')
?>
试试这个:
$collection->getSelect()
->join($this->getTable('faqcat/faqcat'), "faqcat.faqcat_id = main_table.faqcat_id", array(faqcat.*));
您可以通过以下方式查看实际运行以获取集合的sql:
Mage::log($collection->getSelect()->__toString());
Varien_Db_Select类基于Zend_Db_Select,因此是一个很好的参考。我刚刚开始开发magento扩展,我很喜欢它,这是其中的第二部分,我必须从两个表中显示一个网格。呼。 但这并不是那么容易,在网上冲浪了很多次之后,我通过这样做达到了我的目的
$collection = Mage::getModel('linkdirectory/linkdirectory')->getCollection();
$resource = Mage::getSingleton('core/resource');
$collection->getSelect()
->join(
array('lk'=>$resource->getTableName('linkdirectory/linkcategory')),
'lk.cat_id = main_table.cat_id',
array('lk.cat_title','lk.cat_id')
);
/*我的在展示这个*/
/从linkdirectory中选择main_table.,lk.cat_title,lk.cat_id作为main_table内部连接linkcategory作为lk ON lk.cat_id=main_table.cat_id*/
/*打印当前查询的步骤*/
$collection->printlogquerytrue;退出 将select作为字符串获取的替代语法[string$collection->getSelect]。结果是一样的,但有些人觉得语法更简洁。使用u_-toString似乎更健壮,尽管对字符串进行类型化可以有效地工作,但您也可以使用echo$collection->getSelect