简体   繁体   中英

Doctrine - Symfony query error

I do have an error 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 error 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 someone help me? Thanks.

Why not use 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 reference http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html

Try using an alias:

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

Hope this help

它不是MySQL查询它的DQL查询,因此,不要使用“`”字符作为单元格的名称。

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

I guess you could create a query for this through Doctrine. Another way would be to change these in your normal controller 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 is this maybe a command? If so then Palethorn's answer above is a better alternative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM