1

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 : Blackfire profiling output

It seems really unecessary, do you know what configuration could be causing this ?

jiboulex
  • 2,963
  • 2
  • 18
  • 28
  • Maybe this helps: https://stackoverflow.com/questions/28097805/how-does-doctrine-entity-manager-flush-method-works Is the a reason why you are flushing an SELECT? – Foobar Nov 16 '22 at 10:59
  • Thank you for your answer, no I understand how the entityManager works and this code was just to put forward the fact that when no setter was called, the entity was still updated, but I found why – jiboulex Nov 16 '22 at 11:08

1 Answers1

2

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 !

jiboulex
  • 2,963
  • 2
  • 18
  • 28
  • Thank you! God this is magic! I had a decimal field, so I typed it to float, and thus it decided to update a table that I only used to select waaay earlier and I didn't make any changes. Apparently it had to be a string. – Samantha Adrichem Jul 11 '23 at 13:19