2012-12-18 48 views
1

我有一个自定义表starmall_config_shop,列idnameMagento 1.7 collection and joinField

我将自定义属性shop_id添加到产品表中。

我想要得到的namestarmall_config_shop

当我做

$rs = Mage::getModel('catalog/product') 
       ->getCollection() 
       ->addAttributeToSelect('shop_id'); 

$rs->joinField('shop_name','starmall_config/shop','name','id=shop_id' 
    ,NULL,'left'); 

我得到的错误

Unknown column 'at_shop_id.value' in 'on clause'

$rs->printlogquery(true)给出:

SELECT `e`.*, `at_shop_name`.`name` AS `shop_name` FROM `catalog_product_entity` AS `e` 
LEFT JOIN `starmall_config_shop` AS `at_shop_name` ON (at_shop_name.`id`=at_shop_id.value) 

我看到我可能缺少对属性表的连接。

at_shop_id.value从哪里来? 如何获取查询中的name列?

===================

当前工作解决方案是使用:

->addAttributeToSelect('shop_id', 'left') --->工作

->addAttributeToSelect('shop_id') - - >不工作

+0

一眼看上去,你的代码对我来说似乎可以,你正在清理试图加入shop_id属性,但它不在你的sql中。如果有的话,我猜你已经启用了平面目录并需要重新索引,或者缓存清除。 –

+0

刚刚检查,但在配置平面目录中未启用。对于开发,我禁用了缓存。确定删除了缓存文件夹,但查​​询仍然失败。是的,它不是在SQL中,我猜我需要找出如何将自定义产品属性“shop_id”加入到查询中。 – Guus

+0

可能是一个愚蠢的问题,但你确定你删除了正确的缓存目录。我在使用Magento时遇到的第一个问题是我忘记将var目录设置为可写,并在/ tmp /中创建缓存。 –

回答

0

在SYSTEM.LOG我看到:

Recoverable Error: Object of class Mage_Catalog_Model_Resource_Product_Collection could not be converted to string

我搜查,发现有第二个参数:

addAttributeToSelect($attribute, $joinType=false) 

它工作时,我现在使用:

addAttributeToSelect('shop_id', 'left'); 

产生的查询然后是:

SELECT e.*, at_shop_id.value AS shop_id, at_shop_name.name AS shop_name 
FROM catalog_product_entity AS e 
LEFT JOIN catalog_product_entity_int 
    AS at_shop_id 
    ON (at_shop_id.entity_id = e.entity_id) 
    AND (at_shop_id.attribute_id = 973) 
    AND (at_shop_id.store_id = 0) 
LEFT JOIN starmall_config_shop 
    AS at_shop_name 
    ON (at_shop_name.id=at_shop_id.value) 

但我仍然不知道为什么?

0

问题是

'id=shop_id'. 

尝试使用

"entity_id=shop_id" 

如果shop_id是来自表at_shop_name的字段。

+0

我认为'id = shop_id'中的左侧是正确的,因为生成的连接是'LEFT JOIN starmall_config_shop AS at_shop_name ON(at_shop_name.id = at_shop_id.value)'其中'at_shop_name.id'是'PK' starmall_config_shop'表。 – Guus