我正在使用Symfony 4和Doctrine 2.我遇到了这个问题,Doctrine为我生成了一个错误的查询。
我有两个实体,CharacterModel和Character。每个角色模型只能是一个角色,每个角色可以有很多角色模型。所以我在实体中画出这样的关系。所有的getter和setter都已设置好。
CharacterModel.php
class CharacterModel
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var Character
* @ORM\ManyToOne(targetEntity="Character", inversedBy="characterModels")
* @ORM\JoinColumn(name="character_id", referencedColumnName="id")
*/
protected $character;
Character.php
class Character
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="name", type="string", length=30, nullable=false, unique=true)
*/
protected $name;
/**
* @var CharacterModel
* @ORM\OneToMany(targetEntity="CharacterModel", mappedBy="character")
*/
protected $characterModels;
Controller.php这样
$em = $this->getDoctrine()->getManager();
$characterModel = $em->getRepository(CharacterModel::class)->find($id);
// get character used by id
echo $characterModel->getCharacter()->getId();
上面的getId调用可以正常工作,但是当我尝试getName时,它在生成的查询中给出了语法错误。
// this doesn't work
echo $characterModel->getCharacter()->getName();
// Error message
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2 FROM character t0 WHERE t0.id = ?' 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 'character t0 WHERE t0.id = 1' at line 1
我对来自其他实体的其他getter进行了一些测试,除了这个特殊的一个,它们都有效。这是一个错误吗?
答案 0 :(得分:2)
可能会发生此错误,因为Set
是SQL保留字。
在这种情况下,您必须在Doctrine映射中指定和引用表名(或更改实体名称)
let allUsersSet = Set(users)
let newArray = Array(allUsersSet.subtracting(UsersBlockingCurrentUserArray))