Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 SQLSTATE[42000]: Syntax error or access violation: 1064

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;

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

$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?

like image 353
user2993220 Avatar asked Dec 27 '13 09:12

user2993220


1 Answers

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

Docs

Change it to

* @ORM\Table(name="`like`")
like image 86
Mihai Avatar answered Nov 09 '22 13:11

Mihai