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

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

Open
n3o77 opened this issue Dec 25, 2020 · 1 comment
Open

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

n3o77 opened this issue Dec 25, 2020 · 1 comment
Labels

Comments

@n3o77
Copy link

n3o77 commented 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
@PSF1
Copy link

PSF1 commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants