We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Update Primary Keys

Hi All,

i'm getting in trouble with updates for primary keys. Since ORM uses pk to identify its counterpart record, to determine what to update, if I try to update a PK of an object, Phalcon updates the other, if the record does exist.

How can I tell ORM the current and the new pk?

Thanks Gianluca



98.9k

The ORM implements the active record pattern in which an object instance is tied to a single row in the table. Mutating the primary key leads to modify the row on which the object ties to, in a completely different one which itself violates the pattern implementation consistency.

I think you can use the database layer to perform these updates if you still need it. http://docs.phalcon.io/en/latest/reference/db.html



20.4k

Thanks Phaclon for the suggestion, i'll have a try.



100
edited Dec '14

What about for Natural Keys? Like this table:

====================

genres

=====

genre VARCHAR(45)

created_at TIMESTAMP

updated_at TIMESTAMP

====================

genre is the natural key (making it the primary key). But it needs to be updatable. Would this fit a different mold/mindset?



43.9k

Hi,

I'm quiet sure that without INT primary key you want be able to use phalcon ORM. But db layer will be still useable.