[feature request]: allow enum instances to be used as parameters in queries. #6942

Closed
opened 2026-01-22 15:41:53 +01:00 by admin · 1 comment
Owner

Originally created by @azjezz on GitHub (Mar 5, 2022).

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

As of version 2.9, native support for enums has been added ( https://www.doctrine-project.org/2021/05/24/orm2.9.html ), allow for the following:

namespace App\Entity;

use App\Repository\ArticleRespository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ArticleRepository::class)]
class Article
{
    #[ORM\Id, ORM\GeneratedValue(strategy: 'IDENTITY'), ORM\Column(type: Types::INTEGER)]
    public readonly int $id;

    public function __construct(
      #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'articles')]
      #[ORM\JoinColumn(nullable: false)]
      public User $author,

      // more fields

      #[ORM\Column(type: 'string', enumType: ArticleStatus::class)]
      public ArticleStatus $status = ArticleStatus::Draft,
    ) { }
}

one of the limitation with enums is that they are not supported by the query builder.

The following code, results in an Error with message Object of class App\Entity\Enum\ArticleStatus could not be converted to string

$published_articles = $em
  ->createQuery('SELECT a from App\Entity\Article a WHERE a.author = :author AND a.status = :published')
  ->setParameters(new ArrayCollection([
    new Parameter('author', $user),
    new Parameter('published', ArticleStatus::Published),
  ]))
  ->getResult()
;

To get around this problem, you would need to use the enum value instead of the enum instance as follow:

$published_articles = $em
  ->createQuery('SELECT a from App\Entity\Article a WHERE a.author = :author AND a.status = :published')
  ->setParameters(new ArrayCollection([
    new Parameter('author', $user),
    new Parameter('published', ArticleStatus::Published->value),
  ]))
  ->getResult()
;

While the fix is trivial, it is quite annoying as i tend to forget about it all the time.

This issue suggests a new feature to be added to the ORM Query builder, where if a parameter is of type BackedEnum, BackedEnum::$value would be used as value instead of attempting to convert the instance to a string, this should not cause any BC breaks.

Originally created by @azjezz on GitHub (Mar 5, 2022). ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary As of version 2.9, native support for enums has been added ( https://www.doctrine-project.org/2021/05/24/orm2.9.html ), allow for the following: ```php namespace App\Entity; use App\Repository\ArticleRespository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: ArticleRepository::class)] class Article { #[ORM\Id, ORM\GeneratedValue(strategy: 'IDENTITY'), ORM\Column(type: Types::INTEGER)] public readonly int $id; public function __construct( #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'articles')] #[ORM\JoinColumn(nullable: false)] public User $author, // more fields #[ORM\Column(type: 'string', enumType: ArticleStatus::class)] public ArticleStatus $status = ArticleStatus::Draft, ) { } } ``` one of the limitation with enums is that they are not supported by the query builder. The following code, results in an `Error` with message `Object of class App\Entity\Enum\ArticleStatus could not be converted to string` ```php $published_articles = $em ->createQuery('SELECT a from App\Entity\Article a WHERE a.author = :author AND a.status = :published') ->setParameters(new ArrayCollection([ new Parameter('author', $user), new Parameter('published', ArticleStatus::Published), ])) ->getResult() ; ``` To get around this problem, you would need to use the enum value instead of the enum instance as follow: ```php $published_articles = $em ->createQuery('SELECT a from App\Entity\Article a WHERE a.author = :author AND a.status = :published') ->setParameters(new ArrayCollection([ new Parameter('author', $user), new Parameter('published', ArticleStatus::Published->value), ])) ->getResult() ; ``` While the fix is trivial, it is quite annoying as i tend to forget about it all the time. This issue suggests a new feature to be added to the ORM Query builder, where if a parameter is of type `BackedEnum`, `BackedEnum::$value` would be used as value instead of attempting to convert the instance to a string, this should not cause any BC breaks.
admin closed this issue 2026-01-22 15:41:53 +01:00
Author
Owner

@derrabus commented on GitHub (Mar 6, 2022):

Duplicate of #9372

@derrabus commented on GitHub (Mar 6, 2022): Duplicate of #9372
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6942