-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
2.13.2 - 2.13.3 | Enum column - Object of class x could not be converted to x #10066
Comments
This is also likely to be fixed with #10058 . Please confirm |
I am getting the same error, and when I comment out namespace Doctrine\ORM\Query;
class SqlWalker implements TreeWalker
{
public function walkSelectExpression($selectExpression)
{
...
if (! empty($mapping['enumType'])) {
$this->rsm->addEnumResult($columnAlias, $mapping['enumType']);
}
...
}
} Note that for my case, Doctrine tries to typecast namespace Doctrine\DBAL\Types;
class SmallIntType extends Type implements PhpIntegerMappingType
{
...
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value === null ? null : (int) $value;
}
...
} resulting in error However, as shown below, namespace App\Doctrine\Types;
use App\Entity\AccessControl\Acl\Permission;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
final class PermissionType extends Type
{
private const PERMISSION = 'permission';
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
return $platform->getSmallIntTypeDeclarationSQL($fieldDeclaration);
}
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
{
if (null === $value) {
return null;
}
if (\is_int($value)) {
return Permission::createFromValue($value);
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['int', 'null']);
}
public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
{
if (null === $value) {
return null;
}
if ($value instanceof Permission) {
return $value->getValue();
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), [Permission::class, 'null']);
}
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
public function getName(): string
{
return self::PERMISSION;
}
} class Permission
{
private const READ = 0b0000000011;
private const CREATE = 0b0000001100;
private const MODIFY = 0b0000110000;
private const DELETE = 0b0011000000;
private const COWORKER = 0b0100000000; // Not currently used.
private const OWNER = 0b1000000000; // Not currently used.
public function __construct(
private ?PermissionEnum $read=null,
private ?PermissionEnum $create=null,
private ?PermissionEnum $modify=null,
private ?PermissionEnum $delete=null,
private ?bool $restrictToOwner=null,
private ?bool $coworkers=null
) {
}
public function getValue(): ?int
{
if($this->hasNullValue()) {
throw new Exception('Permission has NULL values.');
}
$rs = $this->read->value | $this->create->value << 2 | $this->modify->value << 4 | $this->delete->value << 6;
$rs = $this->coworkers ? $rs | self::COWORKER : $rs & ~self::COWORKER;
return $this->restrictToOwner ? $rs | self::OWNER : $rs & ~self::OWNER;
}
} enum PermissionEnum: int
{
case public = 0b00;
case owner = 0b01;
case restrict = 0b10;
case extra = 0b11;
} |
@NotionCommotion Please provide a small application or a functional test case that reproduces your issue. |
I see fix of this issue in this commit |
See also #10057 |
@derrabus I spent about 4 hours yesterday trying to isolate the issue so I may provide it, but was unsuccessful. Didn't try everything such as checking whether some 3rd party app such as doctrine extensions is also involved. Any thoughts how one best troubleshoots and isolates issues such as this? |
@ThomasGoffart is it resolved now? |
Let me know if it is not resolved and I will reopen this. |
This issue is still on my side ( |
Can you please provide your |
This error occurs with a classic enum. <?php
namespace App\Enum;
enum Color: int {
case Red = 1;
case Green = 2;
case Blue = 3;
} |
Just out of curiosity, when using |
Yes, I tested it and it also happens. |
Do these tests you submitted fail - #10111 ? If they do not, can you please push PR with failing tests? I will work on it then. |
Bug Report
Summary
When having an enum column like this:
This error occurred when you load the entity from a query from the Query Builder (repository):
Workaround
Downgrade
doctrine/orm
to 2.13.1 version while waiting for a fix.The text was updated successfully, but these errors were encountered: