Php 原则2映射-当试图持久化拥有方时,多对多Thow异常
我对教义不熟悉,我被卡住了。任何帮助都将不胜感激。 我创建了两个实体:任务和组,其中组是拥有方,具有多对多关系。我试图坚持他们。任务被持久化,但组出现SQL异常 下面是Task.php的代码:Php 原则2映射-当试图持久化拥有方时,多对多Thow异常,php,symfony,doctrine-orm,many-to-many,sqlexception,Php,Symfony,Doctrine Orm,Many To Many,Sqlexception,我对教义不熟悉,我被卡住了。任何帮助都将不胜感激。 我创建了两个实体:任务和组,其中组是拥有方,具有多对多关系。我试图坚持他们。任务被持久化,但组出现SQL异常 下面是Task.php的代码: <?php namespace AppBundle\Entity; /** * Task */ class Task { /** * @var integer */ private $id; /** * @var string */ private $task; /** * @
<?php
namespace AppBundle\Entity;
/**
* Task
*/
class Task
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $task;
/**
* @var \DateTime
*/
private $dueDate;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $groups;
/**
* Constructor
*/
public function __construct()
{
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set task
*
* @param string $task
*
* @return Task
*/
public function setTask($task)
{
$this->task = $task;
return $this;
}
/**
* Get task
*
* @return string
*/
public function getTask()
{
return $this->task;
}
/**
* Set dueDate
*
* @param \DateTime $dueDate
*
* @return Task
*/
public function setDueDate($dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* Get dueDate
*
* @return \DateTime
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* Add group
*
* @param \AppBundle\Entity\Group $group
*
* @return Task
*/
public function addGroup(\AppBundle\Entity\Group $group)
{
$this->groups[] = $group;
return $this;
}
/**
* Remove group
*
* @param \AppBundle\Entity\Group $group
*/
public function removeGroup(\AppBundle\Entity\Group $group)
{
$this->groups->removeElement($group);
}
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
}
Group.php
<?php
namespace AppBundle\Entity;
/**
* Group
*/
class Group
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Group
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $tasks;
/**
* Constructor
*/
public function __construct()
{
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add task
*
* @param \AppBundle\Entity\Task $task
*
* @return Group
*/
public function addTask(\AppBundle\Entity\Task $task)
{
$this->tasks[] = $task;
return $this;
}
/**
* Add tasks
*
* @param array
*
* @return Group
*/
public function addTasks(array $tasks)
{
foreach ($tasks as $task) {
if (is_a($task, 'AppBundle\Entity\Task')) {
$this->tasks[] = $task;
}
}
return $this;
}
/**
* Remove task
*
* @param \AppBundle\Entity\Task $task
*/
public function removeTask(\AppBundle\Entity\Task $task)
{
$this->tasks->removeElement($task);
}
/**
* Get tasks
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTasks()
{
return $this->tasks;
}
}
控制器
class DoctrineController extends Controller
{
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function setupAction()
{
$group = new Group();
$em = $this->getDoctrine()->getManager();
$tasks = $em->getRepository('AppBundle:Task')->findAll();
$group->setName('personal');
$group->addTasks($tasks);
$em->persist($group);
$em->flush();
echo 'success';
return $this->render('AppBundle:Doctrine:setup.html.twig', array(
));
}
}
例外情况:
An exception occurred while executing 'INSERT INTO group (name) VALUES (?)' with params ["personal"]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group (name) VALUES ('personal')' at line 1
我认为这与表约束有关,但我不知道如何修复它。我很确定我的逻辑有缺陷。如果有人能为我指出正确的方向,并提供一个快速修复方法和一篇文章的解释/链接,解释为什么这样做,我将不胜感激。来自Symfony文档 请注意,您的类名和属性没有映射到 受保护的SQL关键字(如组或用户)。例如,如果您的 实体类名为Group,则默认情况下,表名为 组,这将在某些引擎中导致SQL错误。见教条 关于如何正确转义这些关键字的保留SQL关键字文档 名字。或者,如果您可以自由选择数据库模式, 只需映射到不同的表名或列名。见教条 为数据库和属性映射文档创建类 查看条令文件,了解如何引用这些保留字的建议:
但毕竟,建议不要在类或类属性名称中使用保留字。您使用保留字作为表名
组
,因为条令没有逃避名称,所以您会得到该错误。非常感谢,这一错误超出了我的理解。就在我走进来的时候,感觉我的脚趾被撞伤了。
class DoctrineController extends Controller
{
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function setupAction()
{
$group = new Group();
$em = $this->getDoctrine()->getManager();
$tasks = $em->getRepository('AppBundle:Task')->findAll();
$group->setName('personal');
$group->addTasks($tasks);
$em->persist($group);
$em->flush();
echo 'success';
return $this->render('AppBundle:Doctrine:setup.html.twig', array(
));
}
}
An exception occurred while executing 'INSERT INTO group (name) VALUES (?)' with params ["personal"]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group (name) VALUES ('personal')' at line 1