Magento Collection查询

时间:2015-01-20 09:03:19

标签: magento collections

我创建了新的客户属性'personal_number',现在想要在adminhtml sales_order_grid的新列中显示它。 我已经完成了在网格中显示列的所有内容(在config.xml中重写类),创建了MyName_MyModule_Block_Adminhtml_Order_Grid,其中需要重写_getCollectionClass()和_prepareColumns()。我的问题是在_getCollectionClass()中我需要进行数据库查询以将客户属性数据加入订单集合。 Becouse我是Magento的新手,对我来说,magento-way查询的逻辑非常难以理解。有人可以帮助我以Magento方式编写MySql查询,以获取订单网格中我的客户属性'personal_number'的值:

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

1 个答案:

答案 0 :(得分:0)

您通常无需更改_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/