ManyToMany association can't use IN / OR criteria #5349

Open
opened 2026-01-22 15:05:11 +01:00 by admin · 2 comments
Owner

Originally created by @shaunxq on GitHub (Dec 15, 2016).

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'
Originally created by @shaunxq on GitHub (Dec 15, 2016). ```php 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 ```sql SELECT .... WHERE t.member_id = '111' AND te.entity_type = **'Array'** ``` Criteria method orWhere() doesn't work in many-to-many association as well ```php $criteria = Criteria::create()->where(Criteria::expr()->eq("entity_type", 'system')) ->orWhere(Criteria::expr()->eq("entity_type", 'test')); return $this->messages->matching($criteria); ``` SQL becomes ```sql SELECT .... WHERE t.member_id = '111' AND te.entity_type = 'system' **AND** te.entity_type = 'test' ```
Author
Owner

@lcobucci commented on GitHub (Dec 15, 2016):

@shaunxq thanks for pointing this out.
Could you please create a failing test case for this? You can find examples here: 6e6be3fdd9/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (Dec 15, 2016): @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
Author
Owner

@mpdude commented on GitHub (Mar 28, 2025):

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

@mpdude commented on GitHub (Mar 28, 2025): #11895 might be a fix, but I cannot figure out how to correctly do type conversion there. Help appreciated!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5349