doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.94k stars 2.52k forks source link

CustomType conversion is not working in QueryBuilder Where expression #11698

Open dbannik opened 3 weeks ago

dbannik commented 3 weeks ago

related bug https://github.com/doctrine/orm/issues/9989

dbannik commented 3 weeks ago

how i solve this problem for myself

final class FixSqlWalker extends SqlWalker
{
    public function walkPathExpression($pathExpr): string
    {
        $sql = parent::walkPathExpression($pathExpr);

        if ($pathExpr->type === PathExpression::TYPE_STATE_FIELD) {
            $fieldName    = $pathExpr->field;
            $dqlAlias     = $pathExpr->identificationVariable;
            $class        = $this->getMetadataForDqlAlias($dqlAlias);
            $fieldMapping = $class->fieldMappings[$fieldName] ?? [];

            if (isset($fieldMapping['requireSQLConversion'])) {
                $type = Type::getType($fieldMapping['type']);
                $sql  = $type->convertToPHPValueSQL($sql, $this->getConnection()->getDatabasePlatform());
            }
        }

        return $sql;
    }
}
class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    #[Override]
    public function boot(): void
    {
        parent::boot();

        $this->container
            ->get('doctrine.orm.entity_manager')
            ->getConfiguration()
            ->setDefaultQueryHint(Query::HINT_CUSTOM_OUTPUT_WALKER, FixSqlWalker::class)
        ;
    }
}