User.php;
<?php
namespace AcmeDemoBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="like")
* @ORMInheritanceType("SINGLE_TABLE")
* @ORMDiscriminatorColumn(name="itemtype", type="integer")
* @ORMDiscriminatorMap({
* "1" = "LikeArticle",
* "2" = "LikePage",
* "3" = "LikeSite",
* })
*/
class Like
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="integer")
*/
protected $userid;
/**
* @ORMManyToOne(targetEntity="User", inversedBy="likes")
* @ORMJoinColumn(name="userid", referencedColumnName="id")
*/
protected $user;
}
赞页.php;
<?php
namespace AcmeDemoBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="like_page")
*/
class LikePage extends Like
{
/**
* @ORMColumn(type="integer")
*/
protected $itemid;
/**
* @ORMManyToOne(targetEntity="Page", inversedBy="likes")
* @ORMJoinColumn(name="itemid", referencedColumnName="id")
*/
protected $page;
喜欢文章.php;
namespace AcmeDemoBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="like_article")
*/
class LikePage extends Like
{
/**
* @ORMColumn(type="integer")
*/
protected $itemid;
/**
* @ORMManyToOne(targetEntity="Article", inversedBy="likes")
* @ORMJoinColumn(name="itemid", referencedColumnName="id")
*/
protected $article;
我以这种方式将数据添加到数据库中。我正在尝试从页面实体检索数据。但是,在执行此操作时;
$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
在这种情况下,我该怎么办?为什么不找到项?
就像是一个保留字,转义它或更好地更改您的表名。
文档
将其更改为
* @ORMTable(name="`like`")