Criteria on Collection not working when using symfonys ULID type #6596

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

Originally created by @n3o77 on GitHub (Dec 25, 2020).

I'm using the ULID type from symfony as id as described here:

/**
 * @ORM\Id
 * @ORM\Column(type="ulid", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class=UlidGenerator::class)
 */
private ?Ulid $id;

I have an entity with an ManyToMany collection:

/**
 * @ORM\ManyToMany(targetEntity=Attribute::class, inversedBy="attributeGroups", cascade={"persist"})
 *
 * @var Collection<array-key, Attribute>
 */
private Collection $attributes;

I try to filter this collection with a criteria but it won't find any matches because the ULID isn't passed as binary but as string:

$c = Criteria::create();
$c->andWhere(Criteria::expr()->eq('title', 'test'));

$this->attributes->matching($c);
SELECT
    te.id AS id, te.title AS title
FROM
    attribute te    
    JOIN group_product_attribute t ON t.attribute_id = te.id
WHERE
    t.product_attribute_group_id = '01ETDVSFVRB9Q211CCTGRAPMNY'    
    AND te.title = 'test';

If i don't use the criteria and just access the collection everything works fine and the id is passed as hex:

...
WHERE t0.id = 0x01769BBCBF785A6E20858CD430AB52BE
Originally created by @n3o77 on GitHub (Dec 25, 2020). I'm using the ULID type from symfony as `id` as described [here](https://symfony.com/doc/current/components/uid.html#storing-ulids-in-databases): ```php /** * @ORM\Id * @ORM\Column(type="ulid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UlidGenerator::class) */ private ?Ulid $id; ``` I have an entity with an ManyToMany collection: ```php /** * @ORM\ManyToMany(targetEntity=Attribute::class, inversedBy="attributeGroups", cascade={"persist"}) * * @var Collection<array-key, Attribute> */ private Collection $attributes; ``` I try to filter this collection with a criteria but it won't find any matches because the ULID **isn't** passed as binary but as string: ```php $c = Criteria::create(); $c->andWhere(Criteria::expr()->eq('title', 'test')); $this->attributes->matching($c); ``` ```mysql SELECT te.id AS id, te.title AS title FROM attribute te JOIN group_product_attribute t ON t.attribute_id = te.id WHERE t.product_attribute_group_id = '01ETDVSFVRB9Q211CCTGRAPMNY' AND te.title = 'test'; ``` If i don't use the criteria and just access the collection everything works fine and the id is passed as hex: ```mysql ... WHERE t0.id = 0x01769BBCBF785A6E20858CD430AB52BE ```
admin added the Bug label 2026-01-22 15:35:30 +01:00
Author
Owner

@PSF1 commented on GitHub (Oct 6, 2021):

This work for me:

            $divisionIds = [];
            foreach ($divisions as $division) {
                // This parse any valid ULID string.   <------------
                $ulid = new Ulid($division->getId());
                // This transform ULID to a binary representation.   <------------
                $divisionIds[] = $ulid->toBinary();
            }
            // Add divisions list how parameters.
           // This work ok when we use parent::matching()  <------------
           //  to query in a Doctrine repository context. 
            $criteria->andWhere(Criteria::expr()->in('id', $divisionIds));

$divisions are instances of a Doctrine entity with this ID field:

/**
     * @ORM\Id
     * @ORM\Column(
     *     name="id",
     *     type="ulid",
     *     unique=true
     * )
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class=UlidGenerator::class)
     *
     * @Groups({"administrative_division:read"})
     */
    protected $id;

My Doctrine environment:
Symfony 5.3
doctrine/doctrine-bundle 2.4.2
doctrine/orm: 2.10.1

@PSF1 commented on GitHub (Oct 6, 2021): This work for me: ```php $divisionIds = []; foreach ($divisions as $division) { // This parse any valid ULID string. <------------ $ulid = new Ulid($division->getId()); // This transform ULID to a binary representation. <------------ $divisionIds[] = $ulid->toBinary(); } // Add divisions list how parameters. // This work ok when we use parent::matching() <------------ // to query in a Doctrine repository context. $criteria->andWhere(Criteria::expr()->in('id', $divisionIds)); ``` `$divisions` are instances of a Doctrine entity with this ID field: ```php /** * @ORM\Id * @ORM\Column( * name="id", * type="ulid", * unique=true * ) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UlidGenerator::class) * * @Groups({"administrative_division:read"}) */ protected $id; ``` My Doctrine environment: Symfony 5.3 doctrine/doctrine-bundle 2.4.2 doctrine/orm: 2.10.1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6596