orm icon indicating copy to clipboard operation
orm copied to clipboard

Allow relations to be set with PK array

Open Altreus opened this issue 12 years ago • 3 comments

Cascade-save relations have to be hydrated to models before saving, only to be dessicated into their PKs (or FKs from the other perspective) on save.

It would be helpfully efficient if passing an array of IDs would skip the model, presumably allowing the database itself to catch integrity violations.

I haven't worked out a simple way of doing this, hence vague request instead of specific idea with code attached :)

Altreus avatar Mar 26 '13 09:03 Altreus

It is dangerous to assume the database will enforce integrity. Underlying could be MySQL/MyISAM, which doesn't enforce anything. Also, hydrating to models makes sure the defined observers on those models will be triggered, which will not happen if you bypass the ORM logic.

WanWizard avatar Apr 10 '13 15:04 WanWizard

My thinking was simply to avoid having models at all. Not sure whether it is correct to say that one could simply fake models with IDs in order to get them into the database, but I suspect it is, which means there is really no difference to ORM whether you give it an array of objects with PK values, or simply an array of PK values. There comes a point where creating an object for every related row is so inefficient you get into workarounds.

Altreus avatar Apr 29 '13 07:04 Altreus

Point is that if you do this, you create inconsistent behaviour in ORM, as some things that are supposed to happen no longer happens. It could be that the related model I want to add isn't mine, and/or has internal logic (that I don't want to know) that needs to be executed on insert (like for example an observer_slug). This doesn't happen if you only inject the new FK's.

If you insist, use a DB query for that. If needed, hide the DB logic in a model method.

WanWizard avatar Apr 29 '13 15:04 WanWizard