学说查询(Symfony2)上的语法错误

时间:2013-01-21 10:52:04

标签: symfony doctrine

我在此查询中收到语法错误:

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实体

<?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;
}

}

错误输出

  

执行'UPDATE imsg SET read =?时发生异常?哪里   id =?'用params {“1”:“1”,“2”:1}:

     

SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,在'read ='1'附近使用正确的语法   第1行WHERE id = 1'

有什么想法吗?

2 个答案:

答案 0 :(得分:22)

从MySQL手册:http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - READ是保留字。我猜这就是你得到语法错误的原因。

Doctrine可以自动为您引用列名称:

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

向colum的名称添加反引号 - 取自http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#quoting-reserved-words

答案 1 :(得分:0)

如果您使用yaml,则可以执行此操作

Number:
type: entity
table: `number `