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

时间:2022-09-05 15:48:38

User.php;

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;

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;

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;

I add data to the database in this way. I'm trying to retrieve data from the Page Entity. But, When executing this;

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

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

}

I am getting this error;

我收到了这个错误;

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

In this case what should I do? Why not find itemid?

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

1 个解决方案

#1


13  

like is a reserved word,escape it or better yet change your table name.

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

Docs

文件

Change it to

将其更改为

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

#1


13  

like is a reserved word,escape it or better yet change your table name.

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

Docs

文件

Change it to

将其更改为

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