We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 ?



18.5k
Accepted
answer
edited Mar '14

Try:

$department->getMessages();


7.9k
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);}
    }