mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Bi-directional OneToOne not loaded when using custom DBAL types #5959
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 @dekker-m on GitHub (May 1, 2018).
Originally assigned to: @Ocramius on GitHub.
Some days ago we mapped some legacy database to Doctrine. It went well, however, we ran into an issue today with a bi-directional one-to-one association. The association uses custom DBAL types as join columns. This creates an issue when loading the entity. From what we found, the following is happening:
BasicEntityPersister::loadOneToOneEntity(). Using the DBAL SQL Logger we see that the correct SQL is being executed to load the entity. However, the parameter and/or the type of the parameters are wrong. The parameter is again an UUID in the above mention form, however the type of the parameter is not detected at all. Because an UUID in the above mentioned form does not match the BINARY(16) column in the database, no record is returned.I'm not sure if this is a scenario which is supported by Doctrine and if this would be an improvement or a bug, but from what I can see by looking at the
BasicEntityPersister, it would be possible to load those associations correctly. The reason whyBasicEntityPersister::getTypes()is not recognizing the type of the fields correctly is becauseloadOneToOneEntity()passes an SQL column to theload()method.getTypes()looks at the mapping for object properties instead of SQL columns.I'm not that familiar with the doctrine codebase, but from what I can see there could be two possible fixes to support loading entities with custom types as join column (at least for one-to-one associations).
loadOneToOneEntity()the methodsType::getType()andPersisterHelper::getTypeOfColumn()can be used to get the type of the column and convert the value to a database value using the type's methodconvertToDatabaseValue. This is a solution I tested locally and this seems to work. However, I can understand this might not be the best solution, because theexecuteQuerymethod on the connection has a$typesparameter for a reason. If for some reason the type of the parameter is recognized after all, this would cause the conversion to happen twice.loadOneToOneEntity()is now passing the identifiers toload()using SQL columns. However,load()already seems to take care of the conversion from property names/association names to SQL column names. Instead of using SQL columns in the identifier, why not use the association name of the target class? For example:This seems to load the entities as well, and lets the
load()method take care of the property to SQL column mapping. I tested this solution as well and it seems to work. I can't really think of a downside of this solution, nor of any side effects.I tested this against the following packages:
Could the second solution work without introducing any side effects? If so, I might be able to write a pull request.
@oleg-andreyev commented on GitHub (Oct 5, 2018):
We've faced same issue with custom types (uuid), we've figure out how to handle it by adding
to
\Doctrine\ORM\Persisters\Entity\BasicEntityPersister::getTypes, but:getTypesbecause it's privateBasicEntityPersisterwith own implementation, because it's initialized in directly inUnitOfWork(and is based of other persistent)UnitOfWorkwith own implementation, because it's initialized directly inEntityManager@Ocramius commented on GitHub (Oct 5, 2018):
Replacing/customising is not really what should happen here, @oleg-andreyev: the proper type conversions should happen anyway, and ORM 2.6 includes a lot of fixes to achieve that.
What needs to be done here?
@oleg-andreyev commented on GitHub (Oct 5, 2018):
@Ocramius thanks for responding. I'll try to prepare test case as soon as possible.
@oleg-andreyev commented on GitHub (Oct 5, 2018):
@dekker-m @Ocramius after preparing test case, I've found out that this issue is no longer relevant (at least my case OneToOne to JoinedTable entity) in >= v2.6.0
@Ocramius commented on GitHub (Oct 6, 2018):
Closing as per https://github.com/doctrine/doctrine2/issues/7209#issuecomment-427474070
@dekker-m please submit a test scenario if you manage to reproduce this in 2.6 or newer