0

Edit: I managed to add the city column by changing parent to entity, and the city initially appeared in the Grid, but once we started working in the admin, we would eventually get an error!

I used: protected function _prepareCollection() { $collection = Mage::getResourceModel($this->_getCollectionClass()); $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('po_number')); $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.entity_id', array('city')); $this->setCollection($collection); return parent::_prepareCollection(); }

I successfully added one column into the sales gird by using:

 protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('po_number'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

But when I try to add one more by using the following, I get an error. What am I doing wrong?:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('po_number'));
    $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('city'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

I have the corresponding columns in prepareColumns of course:

$this->addColumn('po_number', array(
        'header' => Mage::helper('sales')->__('PO#'),
        'index' => 'po_number',
    ));

    $this->addColumn('city', array(
        'header' => Mage::helper('sales')->__('City'),
        'index' => 'city',
    ));

Thanks so much in advance for any help!

1 Answer 1

0

I believe this issue lies in the fact that there can be two sales_flat_order_address records, a billing address and a shipping address, for each order. Testing out your situtation I received the following exception:

Item (Mage_Sales_Model_Order) with the same id "341314" already exist";i:1;s:4287:"#0 /vagrant/htdocs/lib/Varien/Data/Collection/Db.php(582):
 Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order)

This is because the join is now creating two rows with the same entity_id for each order, one for the sales_flat_order records joined with sales_flat_order_address of type billing, and one for the sales_flat_order_address of type shipping. Varien_Data_Collection doesn't allow multiple items with the same primary key in the collection (in this case entity_id of the sales_flat_order table). You wouldn't see this issue with just the sales_flat_order_payment because generally sales_flat_order to sales_flat_order_payment is a 1:1 relationship.

TLDR;

I would suggest limiting the join of the sales_flat_order_address to a specific type of address (billing or shipping), this can be done by:

    protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('base_amount_authorized'));
    $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id and sales_flat_order_address.address_type="billing"',array('telephone'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

See the specifically in the condition of the join on sales_flat_order_address:

'main_table.entity_id = sales_flat_order_address.parent_id and sales_flat_order_address.address_type="billing"'

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.