Skip to content

Commit

Permalink
minor #189 [Docs] Explain how to use enums with Doctrine ORM QueryBui…
Browse files Browse the repository at this point in the history
…lder (pluk77)

This PR was merged into the 1.x-dev branch.

Discussion
----------

[Docs] Explain how to use enums with Doctrine ORM QueryBuilder

Add section to explain how to use enum objects with QueryBuilder

Commits
-------

66d9335 [Docs] Explain how to use enums with Doctrine ORM QueryBuilder
  • Loading branch information
ogizanagi committed Jun 3, 2022
2 parents 7d08ee2 + 66d9335 commit 306c78d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Table of Contents
* [Using the Doctrine Bundle with Symfony](#using-the-doctrine-bundle-with-symfony)
* [Mapping](#mapping)
* [Default value on null](#default-value-on-null)
* [Troubleshooting](#troubleshooting)
* [Doctrine ODM](#doctrine-odm)
* [Symfony HttpKernel component](#symfony-httpkernel-component)
* [Symfony Serializer component](#symfony-serializer-component)
Expand Down Expand Up @@ -615,6 +616,56 @@ elao_enum:
Beware that your database platform must support it. Also, the Doctrine diff tool is unable to detect new or removed
values, so you'll have to handle this in a migration yourself.

### Troubleshooting

#### Using Enum objects with QueryBuilder

When using enum objects as parameters in a query made with `Doctrine\ORM\QueryBuilder`,
the enum objects are cast to database values using the `__toString()` method
as the parameter type can not be inferred correctly.

Either explicitly use enum value instead of an instance,
or pass the registered DBAL type as the 3rd parameter in `setParameter()`
to allow the query builder to cast the object to the database value correctly.

I.E, given:

```php
class Card
{
/**
* @ORM\Column(type="suit", length=255)
*/
protected ?Suit $suit = null;
}
```

Use one of the following methods:

```php
private function findByType(?Suit $suit = null): array
{
$qb = $em->createQueryBuilder()
->select('c')
->from('Card', 'c')
->where('c.suit = :suit');
// use a value from constants:
$qb->setParameter('param1', Suit::SPADES);
// or from instances:
// PHP >= 8: Use the value of the enum instance, but check for NULL
$qb->setParameter('suit', $suit?->getValue());
// PHP < 8: Use the value of the enum instance, but check for NULL
$qb->setParameter('suit', $suit ? $suit->getValue() : null);
// Use the 3rd parameter to set the DBAL type
$qb->setParameter('suit', $filter->getSuitType(), 'suit');
// […]
}
```

### Create the DBAL type

First, create your DBAL type by extending either `AbstractEnumType` (string based enum), `AbstractEnumSQLDeclarationType` (if you want to use SQL `ENUM` column definition for string enums) or `AbstractIntegerEnumType` (integer based enum, for flagged enums for instance):
Expand Down

0 comments on commit 306c78d

Please sign in to comment.