Doctrine - Symfony request error

I have a bug and I don't know how to fix it .

$query = $entityManager->createQuery("UPDATE AppBundle:ChangeAPI SET `key`='asd123' WHERE `id` = 1");
$query->execute();

      

My AppBundle: ChangeAPI

/**
 * @ORM\Entity
 * @ORM\Table(name="api")
 */
 class ChangeAPI
 {
     /**
      * @ORM\Column(type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     protected $id;

     /**
      * The date on which the shipment has been created
      *
      * @ORM\Column(type="string", name="key")
      */
     protected $key;

     /** Creates a new standard ride */
     function __construct()
     {
     }
 }

      

And this is my result:

[Syntax Error] line 0, col 31: Error: Expected Doctrine\ORM\Query\Lexer::T_SET, got '`'
QueryException: [Syntax Error] line 0, col 31: Error: Expected Doctrine\ORM\Query\Lexer::T_SET, got '`'
QueryException: UPDATE AppBundle:ChangeAPI SET `key`='asd123' WHERE `id` = 1 

      

Can anyone help me? Thank.

+3


source to share


4 answers


Why not use a query builder:

$entityManager->createQueryBuilder()
    ->update('AppBundle:ChangeAPI', 'c')
    ->set('c.key', ':key')
    ->where('c.id = :id')
    ->setParameter('key', 'asd123')
    ->setParameter('id', 1)
    ->getQuery()
    ->execute();

      



Full link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html

+4


source


Try using an alias:

$query = $entityManager->createQuery("UPDATE AppBundle:ChangeAPI c SET c.key='asd123' WHERE c.id = 1");
$query->execute();

      



Hope for this help

+2


source


It is not MySQL to query the DQL query, so don't use the `` character for the cell name.

$query = $entityManager->createQuery("UPDATE AppBundle:ChangeAPI SET key='asd123' WHERE id = 1");

      

+1


source


I think you could create a query for this via Doctrine. Another way is to change them in normal control methods.

In your controller:

/**
 * @Route("/edit/{id}"), name="app_edit_API")
 * @param ChangeAPI
 */
public function editAPIAction(ChangeAPI $changeAPI)
{
     $changeAPI->setKey('asd123');
     $this->getDoctrine()->getManager()->flush();
}

      

Are you using a form or could it be a command? If so, then Palethorn's answer above is the best alternative.

0


source







All Articles