【问题标题】:Magento joining two tables with different columnsMagento 连接具有不同列的两个表
【发布时间】:2012-01-09 14:44:15
【问题描述】:

我必须为股票警报修改一个 adminhtml 部分。通常对于单个产品,通过此功能检索已订阅警报的注册客户:

protected function _prepareCollection()
{
    $productId = $this->getRequest()->getParam('id');
    $websiteId = 0;
    if ($store = $this->getRequest()->getParam('store')) {
        $websiteId = Mage::app()->getStore($store)->getWebsiteId();
    }
    $collection = Mage::getModel('productalert/stock')
        ->getCustomerCollection()
        ->join($productId, $websiteId);
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

这(据我所知)对具有以下列的表 product_alert_stock 进行查询:

alert_stock_id, customer_id, product_id, website_id, add_date, send_date, send_count, status  

然后将其与客户表连接以检索客户名称。

我们为访客用户(未注册)创建了一个类似的表 product_alert_stock_guest,包含以下列:

alert_stock_id, guest_email, product_id, website_id, add_date, send_date, send_count, status

如您所见,区别在于 guest_email 列替换了 customer_id。是否可以加入(或做类似的事情)这两个表,然后将其显示在同一个网格上?

感谢

卢卡


@JakeTheFish 最后我没有加入,但我检索了与常规股票警报类似的访客股票警报对象,我通过 setData('email', $item->getData('email')) 设置电子邮件字段,然后我通过 addItem 将它们添加到 $collection。我有一个错误: Mage::exception('Mage_Eav', '尝试添加添加无效对象') 当我登录常规库存警报项目时,我有很多信息:http://pastebin.com/z7NpNJrH 取而代之的是自定义客人库存警报项目,我有这个:http://pastebin.com/KKcTeQEH 所以问题是我需要一个 Mage_Customer_Model_Customer 实例来添加。我可以创建一个,但是:

1) 我可以使用常规方法作为 setField 填充该实例的属性(我现在正在查看 API,但我没有看到类似的东西)

2)即使我可以填写属性,如果我设置空白字段会怎样?

【问题讨论】:

    标签: magento-1.4 magento-1.5 magento


    【解决方案1】:

    Magento 提供如下方法:

    joinAttribute
    joinField
    joinTable
    

    但连接的表应该有一些相似的 id。

    基于这两个链接12

    试试这样的:

    $selectFoo = Mage::getResourceModel('model/foo')->getSelect();
    $selectBar= Mage::getResourceModel('model/bar')->getSelect();
    $mergedFooBar = $selectFoo->union(array($selectBar));
    

    或者您可以将元素添加到集合中:

    $collection->addItem($item);
    

    但是,您有 2 个不同的类,我不希望它们如何合并到一个集合中,我想如果您想合并 2 个不同的实体,您应该将这 2 个子类设为第三类,这可以协商两者之间的差异电子邮件和 customer_id。

    【讨论】:

    • tnx 为您的回复。我认为最好的解决方案是加入这两个表,因为它们仅在一列上有所不同。关键是,如果我执行此联接,然后我与客户表进行另一次联接,对带有 customer_id 的条目有什么副作用...?
    • 问题是,您的 guest_email 没有与任何 customer_id 关联。因此,如果有任何影响,那么它将出现在您根本没有 customer_id 的条目上。我建议你有一些第三类来协商guest_email 和customer_id 之间的差异。这意味着这将是另一个模型,其中您具有“loadProperModel”之类的内容,并且您的模型将具有诸如“客户标识符”之类的字段,其中 customerEmail 或 customerName 将在其中。像这样。
    • 最后我没有加入,但我检索了与常规股票警报类似的访客股票警报对象,我通过 setData('email', $item-> 设置电子邮件字段getData('email')),然后我通过 addItem 将它们添加到 $collection。我有一个错误:'Mage::exception('Mage_Eav', 'Attempt to add an invalid object')'
    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多