3

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.

5
  • Should not it be AppBundle.ChangeAPI instead of AppBundle:ChangeAPI? Commented Mar 22, 2017 at 13:26
  • don't use the ` character Commented Mar 22, 2017 at 13:31
  • @SougataBose it's 100% ok with : Commented Mar 22, 2017 at 13:33
  • @Matteo. If i don't use ` without, this will be the error: [Syntax Error] line 0, col 31: Error: Expected Doctrine\ORM\Query\Lexer::T_SET, got 'key Commented Mar 22, 2017 at 13:33
  • Ohk.. I didn't know that. Thanks.. :) Commented Mar 23, 2017 at 5:41

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

8 Comments

[Syntax Error] line 0, col 57: Error: Expected =, <, <=, <>, >, >=, !=, got 'AND'
Edited sample. Where accepts parameters in a different way.
I've tried but.. An exception occurred while executing 'UPDATE api SET key = ? WHERE id = ?' with params ["asd123", 1]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'key = 'asd123' WHERE id = 1' at line 1
I have more then 1 update queries, but i dont understand why this is not working, and the rest works !
Is your key column a string?
|
2

Try using an alias:

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

Hope this help

Comments

1

It's not MySQL query it's DQL query, therefore, don't use "`" character for name of cells.

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

1 Comment

[Syntax Error] line 0, col 31: Error: Expected Doctrine\ORM\Query\Lexer::T_SET, got 'key'
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.