Skip to content
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

Misleading ORMInvalidArgumentException message #6029

Closed
foaly-nr1 opened this issue Sep 13, 2016 · 2 comments
Closed

Misleading ORMInvalidArgumentException message #6029

foaly-nr1 opened this issue Sep 13, 2016 · 2 comments
Assignees
Milestone

Comments

@foaly-nr1
Copy link
Contributor

Given two entities and a random class with similar name (to confuse the developer, but could be anything really):

/** @Entity */
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"all"})
     */
    private $groups;

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity */
class Group
{
    /**
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     */
    private $users;

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

class Group2
{}

If you add an instance of Group2 to User#$groups and try to persist, an exception will be thrown from Doctrine\ORM\UnitOfWork:832:

if (! ($entry instanceof $targetClass->name)) {
  throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry);
}

However, instead of reading like:

Expected value of type "AppBundle\Entity\Group" for association field "AppBundle\Entity\User#$groups", got "AppBundle\Entity\Group2" instead.

It reads like:

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "AppBundle\Entity\User#$groups", got "AppBundle\Entity\Group2" instead.

Simply because the $expectedType isn't set correctly for many-to-many associations in Doctrine\ORM:201:

$expectedType = 'Doctrine\Common\Collections\Collection|array';

if (($assoc['type'] & ClassMetadata::TO_ONE) > 0) {
    $expectedType = $targetClass->getName();
}
@asiermarques
Copy link

+1 this is a severe UX problem. That error message must be changed in order to avoid misunderstandings.

@lcobucci
Copy link
Member

@foaly-nr1 could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing.

You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket

foaly-nr1 pushed a commit to foaly-nr1/doctrine2 that referenced this issue Oct 30, 2017
lcobucci pushed a commit to foaly-nr1/doctrine2 that referenced this issue Nov 26, 2017
@lcobucci lcobucci self-assigned this Nov 26, 2017
@lcobucci lcobucci added this to the 2.6.0 milestone Nov 26, 2017
lcobucci pushed a commit to foaly-nr1/doctrine2 that referenced this issue Nov 26, 2017
maglnet pushed a commit to maglnet/doctrine2 that referenced this issue Oct 10, 2018
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

3 participants