1

I am running some code in my Model, which does the following:

public function beforeSave()
{
  $this->parent_exists = FALSE;
  // search for existing parent..
  $existing_parent = Myparents::model()->findByAttributes(array('email' => $this->email)); //

    if (isset($existing_parent) && is_object($existing_parent))
    {
      // WHERE I AM STUCK...
      // need to disable/override the save() to prevent the INSERT into the table
    } else {
      // proceed as normal with the 'normal' save() method
    }
}

Can anyone explain how I prevent the INSERT query from happening via the save() method when the IF statement is true but if the statement is FALSE proceed as normal with the save()

Any ideas?

lin
  • 17,956
  • 4
  • 59
  • 83
Zabs
  • 13,852
  • 45
  • 173
  • 297

2 Answers2

1

Simply return true or false in beforeSave() like below, if beforeSave() returns false, then insertion does not happen. Also note you need to call the parent implementation as I have done below so events are raised properly.

public function beforeSave()
{
  if(parent::beforeSave()){
      $this->parent_exists = FALSE;
      // search for existing parent..
      $existing_parent = Myparents::model()->findByAttributes(array('email' => $this->email)); //

       if (isset($existing_parent) && is_object($existing_parent))
       {
            // WHERE I AM STUCK...
            // need to disable/override the save() to prevent the INSERT into the table
            return false;
       } else {
            return true;
           // proceed as normal with the 'normal' save() method
       } 
      return true;// return T/F for not set case as well
    } else {
        return false;
    }
}

See http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeSave-detail

Manquer
  • 7,390
  • 8
  • 42
  • 69
  • Note, beforeSave() will not trigger on "bulk" (more than one item) saves. Welcome to Yii. – lin Jul 23 '14 at 19:58
  • the reason for that is way the query is built for single insert and multi insert is different.. every ORM has its quirks activeRecord is no different :) – Manquer Jul 24 '14 at 04:28
1

Make this a validation rule instead of a before save. Remember, beforeSave will only trigger on "single-item"-save actions.

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/ http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeSave-detail

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    return array(
       array('email', 'hasNoParent'),
    );
}


/**
 * Check for parent
 *
 * @param string $attribute
 * @param array  $params
 */
public function hasNoParent($attribute, $params)
{
    $existingParent = Myparents::model()->findByAttributes(array(
        'email' => $this->$attribute
    ));

    if (isset($existingParent) && $existingParent instanceof Myparents){
         $this->addError($attribute, 'your password is not strong enough!');
    }
}
lin
  • 17,956
  • 4
  • 59
  • 83
Zombiesplat
  • 943
  • 8
  • 19