mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Update (change id) OneToMany relation? #6160
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @enima-web on GitHub (Jan 21, 2019).
Originally assigned to: @Ocramius on GitHub.
Actualite Entity :
Comment Entity :
after i insert some data in tables all work perfect :
in table Actualite
in table Comment
I want to change the ID of a iten (id=2) in the Actualite table, i must also change the actualite_id in the related table : like this ==>
in table Actualite
in table Comment
I tried to do this operation with the Api-platform method PUT it doesn't work :
Json request : URL : http://127.0.0.1:8000/api/actualites/2
nothing change !
Also i tried to craeact a costum controller to do this operation :
and
I get Error violation :
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (api.comment, CONSTRAINTFK_9474526CA2843073FOREIGN KEY (actualite_id) REFERENCESactualite(id))How can i change the ID of a item in Actualite entity , and change the actualite_id in the related entity?
How can I do this operation ?
@Ocramius commented on GitHub (Jan 23, 2019):
Changing identifiers (any identifiers) after an object has been persisted is generally not supported by the ORM, nor can be supported in future.
Identifiers are supposed to be stable.
@ancpru commented on GitHub (Apr 3, 2019):
I understand that it's not supported by ORM, but having some kind of "hint" in the relation declaration to generate cascade update/delete/set null in the foreign key would be really helpful.
@Ocramius commented on GitHub (Apr 3, 2019):
We had that, and we removed it explicitly, as a conscious project decision.
@ancpru commented on GitHub (Apr 4, 2019):
And I still do not get the reason for removing it ;)
Yes, it does not make much sense in the context of ORM, but it does make sense for having the whole database scheme description in one place and not having to mess around with custom migration scripts - esp. as the name of the constraints may be unknown because they are generated.
I think "Database Hints" could be separated in an own class and considered as "Scheme information which do not affect ORM and might not be respected by all platforms)
e.g.
@OneToMany(targetEntity="Foo",
mappedBy="bar",
dbHint={@RelationHint(constraintName="fk_xxx", onCascade="all", onUpdate="all")})
@Ocramius commented on GitHub (Apr 4, 2019):
It is incompatible with the basic operative invariants of the ORM, and won't be re-introduced, as it would only encourage its usage.
As mentioned above, you are 100% free to manage the schema on your own, and you should have committed schema migrations regardless of whether you use the ORM or not anyway.
@Ocramius commented on GitHub (Apr 4, 2019):
To be more clear: schema management is secondary in the context of ORM: it is nice to have a managed DB, but every application will eventually outgrow the ORM DSL for schema management.
Use DDL, which is specifically designed for this, if you need to go the extra mile.
@ancpru commented on GitHub (Apr 4, 2019):
Well, I probably have to accept this design decision, but I am not really happy with it as it makes things more complicated than necessary. I completely understand that it's not possible to cover 100 %, but I think it would be nice to cover may be 90 % which are often used - even if they are not important in context of ORM.
Most stuff as to be declared in context of ORM anyway, so a deliberately reduced functinality of ORM schema declaration leads to having to declare it with DDL and ORM. Duplicate work which could be avoided in many cases and could reduce errors.
And yes, I understand the problem of having things in the ORM which do not have an influence on the ORM or do not lead to the expected result. But that was the reason for my suggestion to explicitely name it as schema-hint: Is optional, does not have anything to do with ORM and platform might not be able to handle it.
@Ocramius commented on GitHub (Apr 4, 2019):
ORM is already covering well over the 80% of cases: stretching it further will just amplify edge cases.
If you need something custom, write the customisation explicitly yourself: this will also communicate to other developers whether there is impedance between ORM generated schema and actual schema.
In retrospect, I'd say that the ORM should support even less schema definition nuances than it currently does, because those non-ORM-specific nuances is where the majority of reported issues come from.
@ancpru commented on GitHub (Apr 4, 2019):
Well, In understand that issue: Features cause more possible errors and also cause more possibilities for using them the wrong way, having wrong expectations and thus creating errors on the user side. Bugs are annoying.
But I still think that a "missing feature" is often the biggest bug: No, it does not work wrong in this case. It does not work at all.
What do you mean with with customisation and other developers here? A pure project related customisation or extending Doctrine with additional classes to support this?
My issue is that patching the generated scheme with cascades is something regulary do and would be happy if I could avoid it. IOW: No need to "patch" the scheme if it was generated "right" (in the sense of "how I want it") from the beginning. For me this is no unusual use-case because often things are not just done in an application but also by other people manually in simple SQL. Cascading makes many things much easier in this context.
But yes, things might become easier once the constraints can be named and writing DDL for the minor, but regular modification become easier to read and write.
@Ocramius commented on GitHub (Apr 4, 2019):
Sorry, but you have to look it at the maintenance side of this as well: time is not infinite, especially weekends.
Writing DDL by hand (in migrations)
My response from above (rephrased here) is specifically: "get used to it, because it is normal and also OK to do so".