Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[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 ?



3.8k
Accepted
answer

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