HOW TO prevent deletion of the last record in the model?

Discuss everything related to ApPHP MVC Framework. All versions. Any questions related to the Framework developing or using should be posted to this forum.

Moderators: alexandrleonenko, alexmst

administrator
Site Admin
Site Admin
Posts: 6001
Joined: Jan 7th, '09, 23:18
Contact:

HOW TO prevent deletion of the last record in the model?

Postby administrator » Feb 26th, '14, 18:38

To prevent prevent deletion of the last record in the model you have to re-declare beforeDelete() in your model in s following manner:

Code: Select all

/**
 * This method is invoked before deleting a record (after validation, if any)
 * @param string $pk
 * @return boolean
*/
protected function _beforeDelete($pk = '')
{       
    if($this->count() > 1){
        return true;
    }else{
        $this->_error = true;
        $this->_errorMessage = A::t('core', 'You cannot delete the last remaining record in table {table}!', array('{table}'=>'<b>'.ucfirst($this->_table).'</b>'));
        return false;   
    }
}


Of course, you may use this way to prevent deleting last X records (according to your needs).

Return to “ApPHP MVC Framework {developers/users}”