3

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

1 Answer 1

3

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 }
2
  • thanks :) I implemented my own error object like Mage::getSingleton('custom/statusobject')->setMessage('foo'); but after the redirect this is gone maybe you can also give me a hint or should I open new question?
    – xhallix
    Commented Nov 12, 2015 at 12:01
  • this is global message built in module for diplaying message i think you use that or otherwise you have to use core session. to display. Commented Nov 12, 2015 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.