Skip to main content
Post Closed as "Duplicate" by Fabian Schmengler, 7ochem, Marius
edited tags
Link
Fabian Schmengler
  • 66.1k
  • 25
  • 188
  • 422
Tweeted twitter.com/#!/StackMagento/status/518123488188846082
Source Link
Vic
  • 1.4k
  • 1
  • 18
  • 37

Cancel save() in custom model if condition is met in _beforeSave()

I have a custom model and I want to add a validation in the _beforeSave() method so that if it's a new object, and no customer_id has been set, it shouldn't save the record at all. I did the validations on my pages but that doesn't keep other developers from doing something like:

$myModel = Mage::getModel("myModule/myModel");
$myModel->save();

This shouldn't happen because some values must be set before saving. I want something like:

public function _beforeSave()
{
    parent::_beforeSave();
    if($this->getCustomerId() <= 0)
    {
         #cancel save
    }

    return $this;
}

Can I cancel the save() method?