1
$items = Mage::getModel('sales/order')->getCollection();
          $items->getSelect()->joinLeft('sales_flat_order_item', 'main_table.entity_id = sales_flat_order_item.order_id', array('name'));
           $items->addAttributeToFilter('main_table.created_at', array('like'  => $lastweek));
        $items->addAttributeToFilter('status', $status);           
           $items->addAttributeToFilter('name', array('like' => $searchTerm));


        foreach($items as $item)
    {
//get customer email and name
}

This is my code. I tried using distinct and group but i get blank result for those. When i dont, i get this error. I know the issue is because of duplicate order ids. How can i resolve this?

4

The problem is not because of duplicate order ids, the problem is, that your database query returns one order two time.

After loading, all the items are added to the collection, if an order id is already set, this error is thrown.

What do you want to achieve?

To solve this issue you could just remove

$items->getSelect()->joinLeft('sales_flat_order_item', 'main_table.entity_id = sales_flat_order_item.order_id', array('name'));

But I don't think, this solves your real problem.

  • yes. that does not solve because i want to join those 2 tables. i need to get all orders that were placed a week ago and has productname that is like '$name'. issue is when the same guy has 2 products with the same name it throws this error. does that make sense? – dee May 8 '13 at 13:29
  • Yes it does. Do you know SQL? DISTINCT may help you. Do you need the name or only the order? If you don't need the name, just put an empty array() in the end, so the name is not selected but filtered and you can use distinct to solve the problem - I think. – Fabian Blechschmidt May 8 '13 at 13:39
  • i got the solution using direct sql. but was wondering how to do it with the collections. i tried distinct,i tried group. but none of them worked. they were giving out blank results – dee May 8 '13 at 13:49
  • getSelect()->distinct() – Fabian Blechschmidt May 8 '13 at 14:07
  • 2
    You can also try to switch the select and the join so that the unique colomn from the items is used, as every item has his own unique id there is no duplicate. Other option is to split the select in 2 queries, first getting a list of all unique order_id's from the items table and then use this list to collect the e-mail from the order table. – Vladimir Kerkhoff Jun 7 '13 at 11:41
2

I also had the same problem. I fixed it using distinct(). See the working code below.

$products = Mage::getModel('catalog/product')->getCollection();
$products->getSelect()->distinct('e.entity_id')
        ->joinInner(array('t2' => 'efutures_change_products'), 'e.entity_id = t2.pid', array());

Hope this will help someone.

  • where i can change this, i mean where is the code? – zus Jan 10 '18 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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