New object expression does not convert fields before creating object #6423

Closed
opened 2026-01-22 15:32:56 +01:00 by admin · 0 comments
Owner

Originally created by @jeroenvdheuvel on GitHub (Mar 9, 2020).

Bug Report

Q A
BC Break No
Version 2.7.1

Summary

New object expression is not converting PHP value to SQL using the convertToPHPValueSQL method in a given type. This can break new object construction because wrong arguments are provided (due to not executing a SQL function on the database).

Current behavior

Object creation failed because the wrong argument type is provided, due to not running the convert function on the database.

How to reproduce

Type::addType('FooType', FooType::class);
$dql           = 'SELECT NEW ' . FooNewObject::class . '(e.field) FROM ' . FooEntity::class . ' e WHERE e.id = :id';
$entityManager = $this->_getTestEntityManager();
$query         = $entityManager->createQuery($dql);

self::assertRegExp('/SELECT DatabaseFunction\(\w+\.field\) AS /', $query->getSQL());

/**
 * @Entity
 */
final class FooEntity
{
    /** @Id @Column(type="integer") @GeneratedValue */
    public $id;

    /**@Column(type="FooType") */
    public $field;
}

final class Type extends Type
{
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string
    {
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
    }

    public function getName() : string
    {
        return 'Foo';
    }

    public function canRequireSQLConversion() : bool
    {
        return true;
    }

    public function convertToPHPValueSQL($sqlExpr, $platform) : string
    {
        return sprintf('DatabaseFunction(%s)', $sqlExpr);
    }
}

final class FooNewObject
{
    /** @var string */
    public $field;

    public function __construct(string $field)
    {
        $this->field = $field;
    }
}

Expected behavior

When using the new object expression, convertToPHPValueSQL is being called on types that are being used.

Originally created by @jeroenvdheuvel on GitHub (Mar 9, 2020). ### Bug Report | Q | A |------------ | ------ | BC Break | No | Version | 2.7.1 #### Summary New object expression is not converting PHP value to SQL using the `convertToPHPValueSQL` method in a given type. This can break new object construction because wrong arguments are provided (due to not executing a SQL function on the database). #### Current behavior Object creation failed because the wrong argument type is provided, due to not running the convert function on the database. #### How to reproduce ```php Type::addType('FooType', FooType::class); $dql = 'SELECT NEW ' . FooNewObject::class . '(e.field) FROM ' . FooEntity::class . ' e WHERE e.id = :id'; $entityManager = $this->_getTestEntityManager(); $query = $entityManager->createQuery($dql); self::assertRegExp('/SELECT DatabaseFunction\(\w+\.field\) AS /', $query->getSQL()); /** * @Entity */ final class FooEntity { /** @Id @Column(type="integer") @GeneratedValue */ public $id; /**@Column(type="FooType") */ public $field; } final class Type extends Type { public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string { return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); } public function getName() : string { return 'Foo'; } public function canRequireSQLConversion() : bool { return true; } public function convertToPHPValueSQL($sqlExpr, $platform) : string { return sprintf('DatabaseFunction(%s)', $sqlExpr); } } final class FooNewObject { /** @var string */ public $field; public function __construct(string $field) { $this->field = $field; } } ``` #### Expected behavior When using the new object expression, `convertToPHPValueSQL` is being called on types that are being used.
admin closed this issue 2026-01-22 15:32:56 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6423