【问题标题】:How to join collections in Magento?如何在 Magento 中加入收藏?
【发布时间】:2013-08-14 16:34:15
【问题描述】:

我正在尝试使用产品加入自定义集合,以在管理网格小部件中显示产品名称(不仅仅是 id)。到目前为止,我找不到正确的语法。

我可以通过以下方式检索具有产品名称的产品:

Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name');

我可以通过以下方式检索我的自定义集合:

Mage::getResourceModel('xyz_mymodule/model_collection');

如何加入这两者,使模块集合成为主集合,而 $model->getId() 返回的 id 仍然是我的自定义集合的 id?

【问题讨论】:

    标签: magento join collections grid


    【解决方案1】:

    只是快速更正,我不得不在表名上添加引号: 数组('order_item'=> 'sales_flat_order_item'), getSelect() 也不是必需的,因为第三个参数是属性列表。 最后一个参数指定您要使用的连接类型。

    我的版本是这样的:


    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->join(array('order'=> 'sales/order'),'order.entity_id=main_table.entity_id', array('po_number'=>'po_number', 'imi_customer_email' =>'imi_customer_email'), null,'left');
    $this->setCollection($collection);`
    

    【讨论】:

    • 谢谢,我错过了那些报价 :)
    【解决方案2】:

    这里有一个工作示例:

    $collection = Mage::getModel('sales/order')->getCollection();
    $collection->getSelect()->join( array('order_item'=> 'sales_flat_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku'));
    

    【讨论】:

    • 一旦我把我的桌子放进去,这就像我需要的那样工作。我用过:
    • $collection->getSelect()->join(array('product'=> 'catalog_product_flat_1'), 'product.entity_id = main_table.product_fk', array('*', "product_name" => "product.name", "container_name" => "main_table.name", ) );
    • 硬编码表名?似乎在使用前缀的情况下会中断。应该使用 Mage::getSingleton('core/resource')->getTableName('sales/order') 来利用任何社区/本地覆盖以及处理前缀。
    • @BetoCastillo 您错过了这样一个事实,即有时 magento 的 joinjoinTable 函数会比正常查询减少记录数,例如此代码 $collection->joinTable( array('sfoi' => 'sales/order_item'), 'product_id = entity_id', array( 'last_sold_date' => 'MAX(sfoi.created_at)', 'total_qty_sold' => 'SUM(sfoi.qty_ordered)' ) ); $collection->getSelect()->group('e.entity_id');,如何解决此问题?
    • 感谢您的提示,请随时编辑答案:D @VickyDev
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多