Search code examples
phpdqlsymfony4

Symfony 4 Doctrine Update Queries work as Insert


For the last 2 days, I have been struggling with the simplest update but no luck yet. After I updated to Symfony 4, the update query works as Insert. It always creates a new Entity in the database.

Even

$this->entityManager->flush()

inserts a new row

With DQL

public function updateObject(CategoryGroup $categoryGroup){

        return $this->getEntityManager()->createQuery(

            " update App\Entity\CategoryGroup cg  set cg.name = '".$categoryGroup->getName() ."' where cg.id=".$categoryGroup->getId()

        )
            ->getResult();
    }

Or with Entity itself

/**
     * @param $entity
     * @return null
     */
    public function saveEntity($entity){

        try{
            $this->entityManager->persist($entity);
            $this->entityManager->flush($entity);
        }catch (Exception $e){
            echo $e->getMessage(), EOL;
            return null;
        } catch (OptimisticLockException $e) {
            echo $e->getMessage(), EOL;
            return null;
        } catch (ORMException $e) {
            echo $e->getMessage(), EOL;
            return null;
        }

        return $entity;
    }

No matter what I do, it always accepts the update queries as new records. This was working before but not anymore.

Is it about the settings in Symfony 4? Am I missing something?


Solution

  • I removed everything under vendor and reinstalled the composer. Everything fine now