Criteria is incorrectly serializing values on array field #6299

Open
opened 2026-01-22 15:30:24 +01:00 by admin · 1 comment
Owner

Originally created by @hubertnnn on GitHub (Sep 18, 2019).

Bug Report

Q A
BC Break no
Version 2.6.3

Summary

When trying to use Criteria on array type of field, the value is incorrectly serialized.

Field definition:

    /**
     * @ORM\Column(name="roles", type="array")
     */
    private $roles = [];

Criteria usage:

        $criteria = new Criteria();
        $criteria->where($criteria->expr()->contains('roles', 'ROLE_ADMIN'));

        $em = $this->container->get('doctrine')->getManager();
        $data = $em->getRepository(User::class)->matching($criteria);
        foreach ($data as $row) {
            $row->getRoles(); // Force LAZY_LOAD
        }

Current behavior

Generated SQL contains following where clause:
WHERE t0.roles LIKE 's:12:\"%ROLE_ADMIN%\";'

Expected behavior

Where statement should not be serialized:
WHERE t0.roles LIKE '%ROLE_ADMIN%'
or should be serialized before adding percent signs:
WHERE t0.roles LIKE '%s:10:\"ROLE_ADMIN\";%'

Originally created by @hubertnnn on GitHub (Sep 18, 2019). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.6.3 #### Summary When trying to use Criteria on array type of field, the value is incorrectly serialized. Field definition: ``` /** * @ORM\Column(name="roles", type="array") */ private $roles = []; ``` Criteria usage: ``` $criteria = new Criteria(); $criteria->where($criteria->expr()->contains('roles', 'ROLE_ADMIN')); $em = $this->container->get('doctrine')->getManager(); $data = $em->getRepository(User::class)->matching($criteria); foreach ($data as $row) { $row->getRoles(); // Force LAZY_LOAD } ``` #### Current behavior Generated SQL contains following where clause: `WHERE t0.roles LIKE 's:12:\"%ROLE_ADMIN%\";'` #### Expected behavior Where statement should not be serialized: `WHERE t0.roles LIKE '%ROLE_ADMIN%'` or should be serialized before adding percent signs: `WHERE t0.roles LIKE '%s:10:\"ROLE_ADMIN\";%'`
Author
Owner

@Ocramius commented on GitHub (Sep 18, 2019):

Hmm, seems correct behavior, since the conversion goes through SQL conversions in the DBAL type.

The problem is that it certainly isn't obvious to the consumer, but do imagine doing the same with a DateTime instance: a conversion would most certainly be expected.

EDIT: sorry, didn't mean to close

@Ocramius commented on GitHub (Sep 18, 2019): Hmm, seems correct behavior, since the conversion goes through SQL conversions in the DBAL type. The problem is that it certainly isn't obvious to the consumer, but do imagine doing the same with a `DateTime` instance: a conversion would most certainly be expected. EDIT: sorry, didn't mean to close
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6299