Propel2 icon indicating copy to clipboard operation
Propel2 copied to clipboard

Saving object with modified primary key

Open bre7 opened this issue 9 years ago • 6 comments

I've tried searching for a similar issue but couldn't find any (here at least, I did find something similar but with no clear solution on the forums). How can I save an object if the PK has been modified ? The log shows that new PK is used in the WHERE clause (e.g: Original PK was 25)

[2015-08-09 07:20:43] defaultLogger.INFO: UPDATE author SET ID=55 WHERE author.ID=55 [] []

Sandbox: http://sandbox.propelorm.org/486a545

The only solutions I could think of were:

  • Deleting the old object and saving a new one with the new PK
  • Use a SQL query instead of set... and save()

Thanks :wink:

bre7 avatar Aug 09 '15 05:08 bre7

I think this issue explain another weird behavior with cross-ref and primary keys that cannot be updated once the record has been created.

Maxooo avatar Aug 11 '15 12:08 Maxooo

I think a possible (and the easiest) solution would be to store the Pk when the object is created in another variable called originalPK or something.

bre7 avatar Aug 11 '15 14:08 bre7

Is someone able to make a PR here with suggested changes?

dereuromark avatar Jul 01 '20 13:07 dereuromark

Is anyone on this behaviour? Otherwise, I would try.

Meanwhile: A workaround could be copying the object.

basteyy avatar Jul 30 '22 19:07 basteyy

Go for it

dereuromark avatar Jul 30 '22 20:07 dereuromark

I created a pull request for that. https://github.com/propelorm/Propel2/pull/1896

The checks are failing because of:

Error: dealerdirect/phpcodesniffer-composer-installer contains a Composer plugin which is blocked by your allow-plugins config. You may add it to the list if you consider it safe.
You can run "composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer [true|false]" to enable it (true) or disable it explicitly and suppress this exception (false)
See https://getcomposer.org/allow-plugins

basteyy avatar Aug 01 '22 16:08 basteyy