Criteria behaviour is not consistent across association types #7127

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

Originally created by @davidkmenta on GitHub (Apr 5, 2023).

Bug Report

Q A
BC Break yes
Version 2.14.1

Summary

Criteria behaviour is not consistent across association types.

Current behavior

When using criteria with ManyToMany collection, Doctrine can't properly determine column types and uses different name strategy than when using OneToMany association.

How to reproduce

I've got two separate collections of the same entity:

/**
 * @var Collection<int, Image>
 *
 * @ORM\ManyToMany(targetEntity="Media\Image\Image", cascade={"persist", "remove"}, orphanRemoval=true)
 * @ORM\JoinTable(
 *     name="article_image",
 *     joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true)}
 * )
 */
private Collection $images;

and

/**
 * @var Collection<int, Image>
 *
 * @ORM\OneToMany(targetEntity="Media\Image\Image", mappedBy="parent", orphanRemoval=true)
 */
private Collection $children;

This matching behaves as expected:

public function findMini(): ?Image
{
    $criteria = Criteria::create()->andWhere(Criteria::expr()->eq('size', ImageSizeEnum::MINI))->orderBy(['created_at' => Criteria::ASC]);

    return $this->children->matching($criteria)->first() ?: null;
}

But this one ends with two errors:

Object of class Media\Image\ImageSizeEnum could not be converted to string

Warning: Undefined array key "created_at"

public function getMiniImages(): ArrayCollection
{
    $criteria = Criteria::create()->andWhere(Criteria::expr()->eq('size', ImageSizeEnum::MINI))->orderBy(['created_at' => Criteria::ASC]);

    return $this->images->matching($criteria);
}

It starts to work if I change the created_at (snake_case) column name to createdAt (camelCase) and (obviously) the enum to string.

Expected behavior

Behaviour of criteria should be consistent across all types of associations.

Originally created by @davidkmenta on GitHub (Apr 5, 2023). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes | Version | 2.14.1 #### Summary Criteria behaviour is not consistent across association types. #### Current behavior When using criteria with `ManyToMany` collection, Doctrine can't properly determine column types and uses different name strategy than when using `OneToMany` association. #### How to reproduce I've got two separate collections of the same entity: ```php /** * @var Collection<int, Image> * * @ORM\ManyToMany(targetEntity="Media\Image\Image", cascade={"persist", "remove"}, orphanRemoval=true) * @ORM\JoinTable( * name="article_image", * joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true)} * ) */ private Collection $images; ``` and ```php /** * @var Collection<int, Image> * * @ORM\OneToMany(targetEntity="Media\Image\Image", mappedBy="parent", orphanRemoval=true) */ private Collection $children; ``` This matching behaves as expected: ```php public function findMini(): ?Image { $criteria = Criteria::create()->andWhere(Criteria::expr()->eq('size', ImageSizeEnum::MINI))->orderBy(['created_at' => Criteria::ASC]); return $this->children->matching($criteria)->first() ?: null; } ``` But this one ends with two errors: >Object of class Media\Image\ImageSizeEnum could not be converted to string >Warning: Undefined array key "created_at" ```php public function getMiniImages(): ArrayCollection { $criteria = Criteria::create()->andWhere(Criteria::expr()->eq('size', ImageSizeEnum::MINI))->orderBy(['created_at' => Criteria::ASC]); return $this->images->matching($criteria); } ``` It starts to work if I change the `created_at` (snake_case) column name to `createdAt` (camelCase) and (obviously) the enum to string. #### Expected behavior Behaviour of criteria should be consistent across all types of associations.
Author
Owner

@mpdude commented on GitHub (May 31, 2023):

Could you please provide a full (functional) test case to demonstrate the problem, ideally as a PR?

Have a look at tests/Doctrine/Tests/ORM/Functional/Ticket for examples.

@mpdude commented on GitHub (May 31, 2023): Could you please provide a full (functional) test case to demonstrate the problem, ideally as a PR? Have a look at `tests/Doctrine/Tests/ORM/Functional/Ticket` for examples.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7127