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

[SOLVED] Phalcon\Mvc\Model::update() try to insert

Hi,

I don't get what I'm doing wrong but when I use the update method of my model, it tries to insert a record instead of updating it. And this throw me an exception. Here my sample code (Only these 2 lines are in my method as I'm trying to understand what is wrong):

public function editAction(){
    $user = User::findFirst(array('id=:id:', 'bind' => array('id' => $id)));
    $user->update();
}

Here are the queries catch using a db listener (I omitted the queries made by phalcon to get schema and table informations):

SELECT "users"."id", ... "users"."status" FROM "users" WHERE "users"."id" = :id LIMIT 1

INSERT INTO "users" ("username", "password", "email", "publicname", "accountid", "id") VALUES (?, ?, ?, ?, ?, ?)

BTW : My model is populated correctely with the database values

The exception : SQLSTATE[23505]: Unique violation: 7 ERREUR: la valeur d'une clé dupliquée rompt la contrainte unique « ml_srs_uk » DETAIL: La clé « (email)=(**@.com) » existe déjà .

Can anyone explain what I'm doing wrong ?



5.1k
Accepted
answer

I mixed up between sequences and primary key. Sorry for the error.