8000 "Object could not be converted to string" in UniqueEntityValidator with foreign primary keys · Issue #21274 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

"Object could not be converted to string" in UniqueEntityValidator with foreign primary keys #21274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcverney opened this issue Jan 13, 2017 · 1 comment

Comments

@marcverney
Copy link
marcverney commented Jan 13, 2017
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.1.2

tl;dr

The UniqueEntityValidator makes the wrong assumption that Doctrine's ClassMetaData::getIdentifierValues() can only return scalars:

implode(', ', $class->getIdentifierValues($entity))

Therefore, it crashes when the returned identifiers are objects (associated entities).

Details

When the DoctrineBridge UniqueEntityValidator encounters a non unique value, it builds a constraint violation with it. In case this value is not castable to a string it tries to build one itself:

if (is_object($invalidValue) && !method_exists($invalidValue, '__toString')) {
    $invalidValue = sprintf('Object of class "%s" identified by "%s"', get_class($entity), implode(', ', $class->getIdentifierValues($entity)));
}

If the entity's identifiers are associations (for example a Friendship entity with the two users as composite PK), $class->getIdentifierValues($entity) returns those entities. If they don't have any __toString() method either, the implode() function can't execute and it errors out with the following message:

Error: Object of class [...] could not be converted to string

Not sure what the complete value should be anyway. Recursing one level would produce a rather convoluted string...:

Object of class Acme\AppBundle\Entity\Friendship identified by object of class Acme\AppBundle\Entity\User identified by 123, object of class Acme\AppBundle\Entity\User identified by 456

dmaicher added a commit to dmaicher/symfony that referenced this issue Jan 14, 2017
dmaicher added a commit to dmaicher/symfony that referenced this issue Jan 14, 2017
dmaicher added a commit to dmaicher/symfony that referenced this issue Jan 14, 2017
dmaicher added a commit to dmaicher/symfony that referenced this issue Jan 14, 2017
@dmaicher
Copy link
Contributor

Thanks for the detailed description. I was able to reproduce it and proposed a fix that is similar to what you tested.

The message can indeed get quite long now 😢 But maybe someone has a better idea...

See #21288

fabpot added a commit that referenced this issue Jan 16, 2017
…object primary keys (dmaicher, HeahDude)

This PR was merged into the 3.1 branch.

Discussion
----------

[Doctrine Bridge] fix UniqueEntityValidator for composite object primary keys

| Q             | A
| ------------- | ---
| Branch?       | master / 3.1
| Bug fix?      | yes
| New feature?  |no
| BC breaks?    | no
| Deprecations? |no
| Tests pass?   | yes (fail on php 7.1 unrelated?)
| Fixed tickets | #21274
| License       | MIT
| Doc PR        | -

This PR fixes an issue with the UniqueEntityValidator in case the entity being validated uses a composite primary key via relations to other entities whose classes do not have a `__toString` method.

Commits
-------

b3ced86 [DoctrineBridge] Fixed invalid unique value as composite key
5aadce3 [Doctrine Bridge] fix UniqueEntityValidator for composite object primary keys
@fabpot fabpot closed this as completed Jan 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0