Magento Forum

   
What is the Magento way to determine if X (product, order, custom module db row) exists? 
 
ShopGuy
Guru
 
Total Posts:  462
Joined:  2008-09-07
 

I am looking for a way to determine if something exists. The reason for this is because when adding something to the db I dont want to send insert statements for something that already exists (to prevent duplicates, to conserve sql queries, etc). I am looking for the magento way to do the following:

if (($id product_exists('name''T-Shirt Test')) !== false{
// insert product or do something else such as load the item
}

OR

if ((
$id custom_module_item_exists('custom_column''custom_value')) !== false[
// insert custom module row or do something else such as load the item
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
rrobinson
Jr. Member
 
Avatar
Total Posts:  15
Joined:  2008-05-19
Portland, Or
 

$productMage::getModel('catalog/product')
                ->
getCollection()
                ->
addAttributeToSelect('name')
                ->
addAttributeToFilter'name''T-Shirt Test' )
                ->
getItems();
if( 
count$product ) == )
{
    
//then no matching product
else {
    
//matching product exists
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShopGuy
Guru
 
Total Posts:  462
Joined:  2008-09-07
 

Thanks you are a life saver! I have been trying to ensure my code follows the magento way as closely as possible, so that really helps and makes sense.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top