mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Use integer Id to persist relation #6301
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 @PaulCombal on GitHub (Sep 22, 2019).
Originally assigned to: @Ocramius on GitHub.
Support Question
I'm implementing a REST API, and I would like to implement a quick and easy way to create and persist entities.
Therefore, I created an entity like so, where
$plainis a basic associative array:My frontend input will look something like this, and then decoded into the
$plainarray from above:When flushing, I'm greeted by a totally understandable Doctrine error, telling me that
$parentis an integer and is not an entity as expected.I'd like to mention that here, the "parent" will never be used by the framework, as I'm creating a new entity, all I want to do is write
1in theparent_idcolumn of my database.Regarding the foreign key constraints, it's ok if I get an Exception from the database driver or anything else.
Is there any way I can do this, or what would be the best approach to this problem? Thanks
@Ocramius commented on GitHub (Sep 23, 2019):
Assuming you already know
parentto be a valid foreign key (that respects constraints), you can pass aParentreference to your constructor after having calledEntityManager#getReference()with['id' =>1]as identifier.The ORM will work with that proxy (usually without trying to initialize it).
You must pass an object that is an instance of the referenced class though: in this case, the proxy would fulfill that role.
@PaulCombal commented on GitHub (Sep 23, 2019):
Lord forgive my sins:
Thanks @Ocramius for setting me on tracks, yet I still feel like there must be a more elegant way to put it all together.