orm icon indicating copy to clipboard operation
orm copied to clipboard

ManyToMany association can't use IN / OR criteria

Open shaunxq opened this issue 9 years ago • 2 comments

class Member
{
   ...
    /**
     * @ManyToMany(targetEntity="Message")
     * @JoinTable(name="t_member_message",
     *        joinColumns={@JoinColumn(name="member_id", referencedColumnName="id")},
     *        inverseJoinColumns={@JoinColumn(name="message_id", referencedColumnName="id")}
     *      )
     */
    protected $messages;
...
    publicpublic function getMessages()
    {
        $criteria = Criteria::create()->where(Criteria::expr()->in("entity_type", ['system', 'test']));
        return $this->messages->matching($criteria);
    }
}
Notice: Array to string conversion in doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 91

SQL becomes

   SELECT .... WHERE t.member_id = '111' AND te.entity_type = **'Array'**

Criteria method orWhere() doesn't work in many-to-many association as well

$criteria = Criteria::create()->where(Criteria::expr()->eq("entity_type", 'system'))
                                          ->orWhere(Criteria::expr()->eq("entity_type", 'test'));
        return $this->messages->matching($criteria);

SQL becomes

SELECT .... WHERE t.member_id = '111' AND te.entity_type = 'system' **AND** te.entity_type = 'test'

shaunxq avatar Dec 15 '16 08:12 shaunxq

@shaunxq thanks for pointing this out. Could you please create a failing test case for this? You can find examples here: https://github.com/doctrine/doctrine2/tree/6e6be3fdd92728b26a79576f7f91abe4080e54ff/tests/Doctrine/Tests/ORM/Functional/Ticket

lcobucci avatar Dec 15 '16 09:12 lcobucci

#11895 might be a fix, but I cannot figure out how to correctly do type conversion there. Help appreciated!

mpdude avatar Mar 28 '25 11:03 mpdude