-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Closed
Description
Symfony version(s) affected
7.1.x
Description
when using the UniqueEntity constraint on a dto, as explained here https://symfony.com/blog/new-in-symfony-7-1-expanding-uniqueentity-constraint-to-any-php-class i get
Doctrine\\ORM\\EntityManager::initializeObject(): Argument #1 ($obj) must be of type object, int given, called in \/home\/vagrant\/code\/my-project\/vendor\/symfony\/doctrine-bridge\/Validator\/Constraints\/UniqueEntityValidator.php on line 113
it happens on doctrine/orm >=3.3.0
on doctrine/orm 2.X it does not happen even if the same line of code is executed.
How to reproduce
#[Immutable]
#[UniqueEntity(fields: ['name', 'relatedId' => 'relation'], entityClass: MyEntity::class)]
class MyDto
{
/**
* @param positive-int $relatedId
* @param non-empty-string $name
* @param non-empty-string|null $description
* @param non-empty-string $date
*/
public function __construct(
#[Assert\Positive]
public readonly int $relatedId,
public readonly string $name,
public readonly ?string $description,
#[Assert\Date]
public readonly string $date,
) {
}
}
entities
#[ORM\Entity]
class MyEntity
{
#[ORM\ManyToOne(targetEntity: RelatedEntity::class)]
protected RelatedEntity $related;
}
#[ORM\Entity]
class RelatedEntity
{
#[ORM\OneToMany(targetEntity: RelatedEntity::class)]
protected array $myEntities;
}
then just use a validator to validate an object of MyDto
Possible Solution
No response
Additional Context
with the following object
{
"name": "my-name",
"description": "desc",
"date": "2027-02-02",
"relatedId": 2
}
the error happens at
$em->initializeObject($criteria[$fieldName]);
where
$fieldName = 'relatedId';
$criteria = ['name' => 'my-name', 'relatedId' => 2];
so
$em->initializeObject($criteria[$fieldName]); -> $em->initializeObject(2);
and 2 is not an object indeed.