【问题标题】:Magento Collection QueryMagento 集合查询
【发布时间】:2015-03-18 10:35:00
【问题描述】:

我创建了新的客户属性“personal_number”,现在想在 adminhtml sales_order_grid 的新列中显示它。 我已经完成了在网格中显示列的所有工作(在 config.xml 中重写类),创建了需要重写 _getCollectionClass() 和 _prepareColumns() 的 MyName_MyModule_Block_Adminhtml_Order_Grid。我的问题出在 _getCollectionClass() 中,我需要在其中进行数据库查询以将客户属性数据加入订单集合。因为我是 Magento 的新手,所以我很难理解 magento 方式的查询逻辑。有人可以帮我在下面以 Magento 方式编写 MySql 查询,以在订单网格中获取我的客户属性“personal_number”的值:

SELECT Orders.*, Customers.customer_id, Custumer.personal_namber FROM Orders INNER JOIN Customers ON Orders.customer_id = Customer.customer_id

【问题讨论】:

    标签: magento collections


    【解决方案1】:

    您通常不需要更改_getCollectionClass,而是在_prepareCollection 上的网格上进行连接。即:

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
    
        //we changed mysql query, we added inner join to order item table
        $collection->join(
        // Alias => Table name
        array('customers' => "customer/customer"), 
        // Join condition
        'main_table.customer_id = customer.customer_id', 
        // Fields to select
        array('personal_number'=>'personal_number');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
    

    取自这里,查看全文以获得更多帮助:http://inchoo.net/magento/how-to-extend-magento-order-grid/

    【讨论】:

    • 这是我的复制/粘贴错误...我提到了 _prepareCollection()...但我不知道如何为 INNER JOIN 客户编写 $collection->join('some_query') Orders.customer_id = Customer.customer_id 获取我的自定义属性值personal_number
    • 用客户表的连接编辑了答案并添加了一些 cmets,希望它可以工作或帮助您获得解决方案。
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2013-05-05
    • 2011-10-05
    • 1970-01-01
    • 2021-02-23
    • 2016-08-07
    相关资源
    最近更新 更多