Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Access model messages in a controller

in a model :

$this->hasMany('id', 'Employees', 'dpt_id', array('alias' => 'DepartmentEmployees', 'foreignKey' => array('message' => 'You cannot delete the department as there are employees assigned.')));

in a controller i try to delete a record that violates the above foreign key
and the action is rejected as expected

    $success = $department->delete();
    if($success == TRUE)
    {
        $this->flashSession->success($department->name.' department deleted successfully.');
    }
    else
    {
        $rejection_reason = '??'; // I need the message  here
        $this->flashSession->error(' Could not delete '.$department->name.' department because '.$rejection_reason);
    }

the question is : within a controller how can i get the foreign key message ?



11.2k
Accepted
answer
edited Mar '14

Try:

$department->getMessages();


5.6k
edited Mar '14

thank you , that worked fine
i post the code for future use

    $success = $department->delete();
    if($success == TRUE)
    {
        $this->flashSession->success($department->name.' department deleted successfully.');
    }
    else
    {
        $messages = $department->getMessages();
        foreach($messages as $msg){$this->flashSession->error($msg);}
    }