Doctrine: How should I update my entity-saving function?

In my Symfony app, I have a function that saves a record using Doctrine:

public function save(Car $car): void
{
    $this->getEntityManager()->persist($car);
    $this->getEntityManager()->flush($car);
}

But in recent days, I get a lot of log entries like this:

Calling Doctrine\ORM\EntityManager::flush() with any arguments to flush specific entities is deprecated and will not be supported in Doctrine ORM 3.0.

What should I do instead?

Upvotes: -2

Views: 77

Answers (2)

swawge
swawge

Reputation: 73

There is a potential issue with the flush() method usage in Doctrine. It worked for me when I removed the $car parameter from flushing

public function save(Car $car): void
{
   $this->getEntityManager()->persist($car);
   $this->getEntityManager()->flush();

}

so it looks like passing an entity to flush() is not supported in newer versions of Doctrine and will produce an error

Upvotes: -1

Khribi Wessim
Khribi Wessim

Reputation: 720

The deprecation warning indicates that passing specific entities to EntityManager::flush() is deprecated in Doctrine ORM and will be removed in version 3.0. Instead of flushing specific entities, you should call flush() without arguments. This flushes all pending changes in the persistence context managed by the EntityManager :

public function save(Car $car): void
{
    $entityManager = $this->getEntityManager();
    $entityManager->persist($car);
    $entityManager->flush(); // Flush all changes
}

Upvotes: 2

Related Questions