Phalcon PHP error handling

0 votes
193 views
asked Jul 29, 2016 in Phalcon by Robin

When I try to save my model like if( !$myModel->save() )

I have to surround in try catch my condition if I want to get the errors messages...

I can't seem to check if the save() is false, because this will not return the errors.

How can I do to get $myModels->getMessages() without using try catch ?

I want to do like this example and return json_encode if the save doesn't work.

1 Answer

0 votes
answered Aug 2, 2016 by phalconizer

https://docs.phalconphp.com/en/latest/reference/models.html#creating-updating-records

<?php

$robot       = new Robots();
$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

if ($robot->save() == false) {
    echo "Umh, We can't store robots right now: \n";
    foreach ($robot->getMessages() as $message) {
        echo $message, "\n";
    }
} else {
    echo "Great, a new robot was saved successfully!";
}

Related questions

0 votes
1 answer 299 views
0 votes
0 answers 215 views
asked Aug 19, 2016 in General by Shed
+1 vote
1 answer 115 views
+2 votes
1 answer 108 views
asked Feb 9 in General by Marc Lentin
0 votes
2 answers 181 views
asked Nov 24, 2016 in General by srinivasulu_yagnam (200 points)
...