For a backend module I have the need to check if a product id is valid, that is: is there a product with that id? I’ve found two solutions for that, but I’m not very happy with either one:
-
Directy query the
catalog_product_entitytable. Very fast but definitely not very elegant and I fear there might be issues I’m not aware of. -
Use the following code:
$product = Mage::getModel('catalog/product')->load($productID) if ($product->getId()) { //valid id } else { //not a valid id }This should work but it’s painfully slow, because I have to check several IDs at once. And since I don’t need the actual product data, it doesn’t really make sense to load it.
Any better suggestions?
Thanks benmarks and james, you’ve pointed me in the right direction and I just combined your approches:
For some reason, getAllIds() returns Ids as strings rather than ints, but that’s fine.