Here's a starting point:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($productIdsArr)
->joinTable(
array('prod_categ_assoc' => 'catalog/category_product'),
'product_id=entity_id',
array('categ_id' => 'category_id', 'position'),
null,
'left'
);
$result = array();
// play with the joined columns
foreach ($collection as $product) {
$key = $product->getId() . "-" . $product->getCategId();
$result[$key] = $product->getPosition();
}
var_dump($result);
// check the resulting SQL with this
var_dump($collection->getSelect()->__toString());
You may have noticed that I used categ_id
as an alias to category_id
. That's because Mage_Catalog_Model_Product::getCategoryId()
is already implemented (and probably used in other places). Better stay safe and use our own attribute - categ_id
.
You'll be interested in Mage_Catalog_Model_Resource_Product_Collection::addIdFilter()
and Mage_Eav_Model_Entity_Collection_Abstract::joinTable()
.
PS: You should check the collection query string before the foreach
, too. It'll be very different from the one after the loop. Magento hooks in after collection loading with some other logic. Just a tip.
Update
I tested on a vanilla installation, and, if you have a product assigned to more than one category, I think the above query will definitely fail with Object with the same id "<id>" already exist
. The collection ID field is the product ID, and that must be unique throughout the collection.
Some 10 minutes later...
Yup, it throws. This is the best I could come up with so that you don't see any SQL in there:
// $collection is the above collection object
$resultArr = $collection->getConnection()->fetchAll($collection->getSelect());
But this collection is not properly set - other SQL logic is applied when you load the collection (remember my tip?), but you can't load it now, because it will throw. Still want convention?
you should use also the following tables and fields in your query:
sales_flat_order_item [quote_item_id]
sales_flat_quote_item [product_id]
Best Answer
This seems to have done the trick