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

Why save()/update() doesn't save and returns no error

Hey guys,

I'm trying to update my table with save() or update() but something is wrong. Everytime that I send a post request with the new information that I want to update in my database, a JSON response is sent back to my brownser but this JSON response should just be sent when the update is sucessful and on my case nothing is changing on my database. Why save() is returning true? *My table has a primary key

Here is my code:

if ($this->request->isPost()) {
$jsonParsed = $this->request->getJsonRawBody();
    $this->view->disable();
    $users->user_id = $auth["user_id"];
    $users->user_username = $jsonParsed->username;

    if ( $users->save() == false ) {

            foreach ($users->getMessages() as $message) {

                echo $message->getMessage();

            }

    } else {

            $array = array("resposta" => "ok");

            $response = new \Phalcon\Http\Response();
            $response->setHeader("Content-Type", "application/json");
            $response->setContent(json_encode($array));
            $response->send();

        }
}

If you use metadataCache service in you DI, and have change any table fileds after that, make sure to remove cache folder (or just user's table cache file) before runnig your code.

Actually I don't use metadataCache in my DI. But anyway I cleaned cache and still not working.

Thank you

Log the queries with Event Manager and see what is happening under the hood...


// Listen all the database events
$eventManager->attach('db', function ($event, $connection) {
    if ($event->getType() == 'afterQuery') {
        echo $connection->getSQLStatement();
    }
});
edited Aug '15

Typically in cases like this, I would put an exit("Test") line in after if ( $users->save() == false ) {, to see if that condition was actually being met