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

Replace Model's "system" messages thrown by create/update.

I found that when Model::create() fails then no one known model's event fires. Only one thing that you cat get is the message "Record cannot be created because it already exists" or etc. Not the question - how to replace this message? I just want to translate it. Maybe I missing something? Of course, I could write some extra functions to check existence of the record, then write message if needed, then call Model::save()... But after a such act, what Model::create() exists for?



98.9k

You can override the getMessages method:

<?php

class Robots extends Phalcon\Mvc\Model
{
    public function getMessages()
    {
        $messages = array();
        foreach (parent::getMessages() as $message) {
            switch ($message->getType()) {
                case 'InvalidCreateAttempt':
                    $messages[] = 'Enregistrement ne peut pas être créé car il existe déjà';
                    break;
                case 'InvalidUpdateAttempt':
                    $messages[] = 'Enregistrement ne peut pas être mis à jour car il existe déjà';
                    break;
                case 'PresenceOf':
                    $messages[] = 'Le champ est obligatoire';
                    break;
            }
        }
        return $messages;
    }
}


32.5k

Ou, is it meaning that each message type is related to one message? I just thought there exist several messages for each type... ^_^ Thank! You're the best!