4

Im getting a syntax error on this query:

protected function _getimsg($id)
{
    $imsgRepository = $this->getDoctrine( )->getRepository( 'DonePunctisBundle:Imsg' );
    $imsg = $imsgRepository->findOneBy(array('to' => $id, 'read' => 0 ));
    if($imsg) {
        $em = $this->getDoctrine()->getEntityManager();
        $imsg->setRead('1');

        $em->persist( $imsg );
        $em->flush( );

        return $imsg->getContent();
    } else {
        return '';
    }

}

imsg Entity

<?php

namespace Done\PunctisBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Imsg
 *
 * @ORM\Table(name="imsg")
 * @ORM\Entity
 */
class Imsg
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="to", type="string", length=25)
 */
private $to;

/**
 * @var string
 *
 * @ORM\Column(name="content", type="string", length=255)
 */
private $content;

/**
 * @var integer
 *
 * @ORM\Column(name="read", type="integer", length=1)
 */
private $read;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set To
 *
 * @param string $to
 * @return Page
 */
public function setTo($to)
{
    $this->to = $to;

    return $this;
}

/**
 * Get to
 *
 * @return string 
 */
public function getTo()
{
    return $this->to;
}

/**
 * Set content
 *
 * @param string $content
 * @return Page
 */
public function setContent($content)
{
    $this->content = $content;

    return $this;
}

/**
 * Get content
 *
 * @return string 
 */
public function getContent()
{
    return $this->content;
}

/**
 * Set read
 *
 * @param integer $read
 * @return Imsg
 */
public function setRead($read)
{
    $this->read = $read;

    return $this;
}

/**
 * Get read
 *
 * @return integer 
 */
public function getRead()
{
    return $this->read;
}

}

The error output

An exception occurred while executing 'UPDATE imsg SET read = ? WHERE id = ?' with params {"1":"1","2":1}:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = '1' WHERE id = 1' at line 1

Any ideas?

2 Answers 2

24

From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - READ is a reserved word. I'm guessing that's why you're getting the syntax error.

Doctrine can automatically quote the column names for you:

<?php
/** @Column(name="`number`", type="integer") */
private $number;

Add backticks to the colum's name - taken from http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#quoting-reserved-words

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

1 Comment

Thanks for this, I had the exact same issue with the exact same keyword ("read"), and was on my way to pull my entire app apart before I saw your answer.
0

if you use yaml, you can do this

Number:
type: entity
table: `number `

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.