Translate direct SQL query into collection model usage
-
30-10-2019 - |
سؤال
I've got this query:
SELECT * FROM 'catalog_category_product' WHERE 'product_id' IN ('.$IDs.')
where $IDs
I get the following way: $IDs = implode(',', $products);
and $products
is an array of product ids.
Now I want to do the same thing, but through the use of a model collection. I've got so far with these examples:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('product_id');
$collection->addAttributeToSelect('category_id');
$collection->addAttributeToSelect('position');
$collection->addFieldToFilter(array('attribute'=>'product_id','in'=>array($products)));
and
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('product_id');
$collection->addAttributeToSelect('category_id');
$collection->addAttributeToSelect('position');
$collection->addFieldToFilter(array(
array('attribute'=>'product_id','in'=>array($products)),
));
But I get exception on the last function call, in both cases.
P.S. I need to acquire these 3 attributes at once (as the SQL query would do), but not iterating for each value in $products
because I am dealing with thousands of products.
لا يوجد حل صحيح
لا تنتمي إلى magento.stackexchange