mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Throwing __toString implementation makes Query(Builder) crash #5835
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 @ro0NL on GitHub (Jan 9, 2018).
Originally assigned to: @Ocramius on GitHub.
Hi,
I have a
Idvalue object withequals() + isEmpty() + __toString()implementation. I'd like to vary empty ID's on beingnullyes/no; the constructor signature looks like__construct(string $id = null).In theory
string("")is a known ID value, thus not empty. But this is what the issue is about yes :)For now im throwing a
LogicExceptionin__toStringin case the ID value is empty (null), as i dont want to cast such a value to string (as mentioned; an empty string is a different value, technically).Design-wise all good; but i noticed i ran into this somewhat unexpected errors here and there:
^ Happening with
persist()^ Happening with e.g.
IDENTITY(deriving_entity.derived_entity) = :entity_object(A) Does it need a fix on doctrine side? Or is this not a supported thing or so? In general i think we should just try/catch the casts 🤔 and default to
null.(B) Should i avoid the trow and threat
null | string("")equally? Thus both empty.(C) Should i avoid the
string $id = nullsignature as a one-size-fits-all for scalars; i.e. go withId::fromString(), Id::fromIntand decide "isEmpty" from there? (That sounds reasonable actually).(D) If i do "B" could that lead to unexpected clauses? As we query with
WHERE field = ""instead offield IS NULL(E) Should i filter out the empty ids myself (i.e. cast to null beforehand). That's a real hassle for nested derived entities etc.
@ro0NL commented on GitHub (Jan 9, 2018):
In general the bigger picture is to have
getId(): Id(not?Idnor?int).@Ocramius commented on GitHub (Jan 9, 2018):
@ro0NL
__toString()cannot throw - that is a PHP language limitation@ro0NL commented on GitHub (Jan 9, 2018):
@Ocramius You're right! Totally overlooked the
must not throw an exceptionpart 😞In general what do you suggest to safely have
getId(): Id? I believe currently doctrine requires a nullable API for PK values.. no? Basically im seeking a VO design for the PK in case it's unknown beforehand (i was going fornew Entity(new Id())).@Ocramius commented on GitHub (Jan 9, 2018):
@ro0NL doctrine doesn't use your getter, so you are free to use whatever you like there, but the property must be set to something stringable and not
null@ro0NL commented on GitHub (Jan 9, 2018):
Yep, im figuring it out :)
In case an empty id is passed to the constructor i just set
$this->id = nulland ignore the VO. That should do!Thanks a lot :) needed this.
@Ocramius commented on GitHub (Jan 9, 2018):
👍