Search code examples
phpmysqlphalcon

Phalcon MySQL UPDATE not working


I am trying to create RESTful API and I am trying to update my MySQL database row through Phalcon via pure queries. But I run into the issue that the query is runned Phalcon status->success() returns true but when I look into the db values are the same as before.

I tried to change from SQL queries to using model->update() but has the same issue. Both values country_id and university_id are integers.

Any clue or workaround?

My POST handling function:

$app->post("/api/user/university/{id:[0-9]+}", function($id) use ($app){

    $university_id = $app->request->getPost("university_id");

    $country_id = universities::findFirstById($university_id)->country_id;

    $phpql = "UPDATE users 
                SET university_id=:university_id:,
                    country_id=:country_id:
                WHERE 
                    id=:user_id:";

    $status = $app->modelsManager->executeQuery($phpql, array(
        'user_id' => $id,
        'university_id' => $university_id,
        'country_id' => $country_id     
    ));


    $response = new Response();

    if($status->success() == true){
        $response->setStatusCode(201, "Created");


        $response->setJsonContent(
            array(
                'status' => 0
            )
        );
    } else {

        $response->setStatusCode(409, "Conflict");

        $errors = array();
        foreach ($status->getMessages() as $message) {
            $errors[] = $message->getMessage();
        }

        $response->setJsonContent(
            array(
                'status'   => 'ERROR',
                'messages' => $errors
            )
        );
    }
    return $response;
});

Solution

  • Is university_id and country_id part of PK ? If yes then phalcon don't allow to change primiary keys. If you need such behaviour remove those columns from primiary key and use uniquness validator.