php - Why doctrine update entities when no setter has been called and change has been made?
PHP 8.1 Symfony 5.4 Doctrine 2.10
I'm profiling a PHP command (with blackfire) with dummy code for testing :
class MyCommand extends Command
{
...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$query = $this->entityManager->createQuery(
"SELECT t from App\Entity\MyEntity t"
);
$query->setMaxResults(20);
$i = 0;
foreach ($query->getResult() as $transaction) {
$i++;
}
$output->writeln($i);
$this->entityManager->flush();
return self::SUCCESS;
}
...
}
So as you can see, no change made, no setter called.
But when I profile my command, I can see that doctrine made as many updates as there are entities :
It seems really unecessary, do you know what configuration could be causing this ?
Answer
Solution:
Ok so I found why, it is because I had unmatching types between my entity properties and the Column type annotation.
Before :
/**
* @ORM\Column(type="string", nullable=true)
*/
private int $myField;
After
/**
* @ORM\Column(type="string", nullable=true)
*/
private string $myField;
So I guess that when the entity is created, the property has to be cast or something like that which causes a change detected by the orm !
Source