Doctrine2 SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2013-12-27 09:37:21

标签: mysql sql symfony doctrine-orm entity-relationship

user.php的;

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="like")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="itemtype", type="integer")
 * @ORM\DiscriminatorMap({
 *  "1" = "LikeArticle",
 *  "2" = "LikePage",
 *  "3" = "LikeSite",
 * })
 */
class Like
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $userid;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="likes")
     * @ORM\JoinColumn(name="userid", referencedColumnName="id")
     */
    protected $user;
}

LikePage.php;

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="like_page")
 */
class LikePage extends Like
{
    /**
     * @ORM\Column(type="integer")
     */
    protected $itemid;

   /**
     * @ORM\ManyToOne(targetEntity="Page", inversedBy="likes")
     * @ORM\JoinColumn(name="itemid", referencedColumnName="id")
     */
    protected $page;

LikeArticle.php;

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="like_article")
 */
class LikePage extends Like
{
    /**
     * @ORM\Column(type="integer")
     */
    protected $itemid;

   /**
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="likes")
     * @ORM\JoinColumn(name="itemid", referencedColumnName="id")
     */
    protected $article;

我以这种方式向数据库添加数据。我正在尝试从Page Entity中检索数据。但是,执行此操作时;

$likes = $page->getLikes();
foreach($likes as $like) {

}

我收到了这个错误;

An exception occurred while executing 'SELECT t0.id AS id1, t0.userid AS userid2, t0.itemid AS itemid3, t0.userid AS userid4, t0.itemid AS itemid5, t0.itemtype FROM like t0 WHERE t0.itemid = ? AND t0.itemtype IN ('1')' with params [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 'like t0 WHERE t0.itemid = '1' AND t0.itemtype IN ('1')' at line 1

在这种情况下,我该怎么办?为什么找不到itemid?

1 个答案:

答案 0 :(得分:14)

就像是一个保留字,逃避它或者更好地改变你的表名。

Docs

将其更改为

* @ORM\Table(name="`like`")