Magento Collection加入不同模块之间

时间:2010-09-02 12:48:31

标签: php collections magento

我一直在研究一个有管理员后端的自定义模块。到目前为止,它一直工作得很好,就像客户模块一样,但现在我坚持这一点。我的自定义表保存了客户ID,我可以在网格的每一行显示客户名称。到目前为止,我能够显示ID,我知道我应该准备好这个系列,但我没有任何线索。如果我使用join,它只适用于同一模块的模型。我试过这个:

$collection = Mage::getModel('mymodule/mymodel')->getCollection()->getSelect()
    ->join(
        'customer_entity',
        'main_table.customer_id = customer_entity.entity_id', array('customer_name' => 'email'));

如果我打印查询看起来像这样

SELECT `main_table`.*, `customer_entity`.`email` AS `customer_name` FROM 
`uhma_comunidad_question` AS `main_table` INNER JOIN `customer_entity` ON 
main_table.customer_id = customer_entity.entity_id

但是没有返回任何内容,该集合为null,如果我复制此sql并将其粘贴到phpmyadmin中它完美无缺,我在这里缺少什么

感谢您的帮助。

2 个答案:

答案 0 :(得分:9)

很简单,如果你对getSelect()使用链接它不起作用,只需添加另一行

$collection = Mage::getModel('mymodule/mymodel')->getCollection();
$collection->getSelect()
    ->join(
    'customer_entity',
    'main_table.customer_id = customer_entity.entity_id', array('customer_name' => 'email'));

它完美无缺

答案 1 :(得分:1)

$collection = Mage::getModel('mymodule/mymodel')->getCollection()->getSelect()
->join(
    'customer_entity',
    'main_table.customer_id = customer_entity.entity_id', array('customer_name' => 'email'));