Magento – How to attach order status to order item collection

collection;order-statusorders

I'm loading a collection of order items like this:

    $collection = Mage::getResourceModel('sales/order_item_collection')
        ->addAttributeToSelect('*');

And I'd like to join to add the order's status to the collection, so that I can filter out incomplete orders via addFieldToFilter('status', 'complete').

How do I join these two things so that I can filter on the parent order's status?

I would paste my various attempts but I think that would probably just add confusion.

Best Answer

You can use the join method on the getSelect object

$collection = Mage::getResourceModel('sales/order_item_collection')
        ->addAttributeToSelect('*');

$collection->getSelect()->join( array('orders'=> sales_flat_order), 'orders.order_id = main_table.order_id', array('orders.status')); // join on orderid and retrieve the status column

Small side note, it's better to specifically state which fields you need instead of using addAttributeToSelect('*')