Magento : How to find out if a record already exists in a database or not ?

To check whether record exist in database or not, you can use following query:

<?php
$collection = Mage::getModel('sample/sample')->getCollection()->addFieldToFilter('field_name', $value_to_check)->getFirstItem();
if($collection->getId()){
   //executing this block if record found in database
}else{
   //executing this block if record not found in database
}

?>
Tags:  ,

Comments

thedotwriter 01-02-2013, 23:28

It’s not working for me since the Sample model simply doesn’t exist on magento installation (1.7)… If this is what I think, it could be useful.

Can you tell me a little more about that model? I didn’t found anything after a quick search…

Cheers

Reply
Alonzo 04-06-2013, 07:05

Hi there, just wanted to mention, I enjoyed this article.
It was helpful. Keep on posting!

Reply

Leave a Reply



four − = three