Check if model column exists before saving in database
-
13-12-2019 - |
Question
I want to insert a column into my db table, but only if it does not exist, yet.
So I thought I'd check if the model with the specified email exists, and if not I perform save()
method.
Here is what I do:
$customerModel = Mage::getModel('custom/customerobject');
$loadedModel = $customerModel->load($RequestParams['email'],'email');
$customerIsUnique = empty($loadedModel->getData('email'));
if($customerIsUnique)
{
// Do stuff
}
Is this the common way how it's done in magento or is there a better way, like
$customerModel->saveOnlyIfItDoesNotExistOrAnyOtherAwesomeMethod()
?
Thanks in advance
Solution
you check it like that..
$customerModel=Mage::getModel("custom/customerobject")->getCollection()
->addFieldToFilter('email', array('eq' => $RequestParams['email']));
if(count($customerModel)==1)
{
Mage::getSingleton('core/session')->addError('Email Already Registered');
$this->_redirect('*/*/');
return;
}else { // do your code here }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange