php - doctrine 2 mapping - many to many thows exception when trying to persist owning side -
i'm new doctrine , got stuck. appreciated. created 2 entities: task , group many many relation group being owning side. tried persist them. task gets persisted, group trows sql exception.
here code task.php:
<?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(); } /** * 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; } /** * 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; } /** * 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); } /** * groups * * @return \doctrine\common\collections\collection */ public function getgroups() { return $this->groups; } }
task mapping
appbundle\entity\task: type: entity table: task id: id: type: integer generator: { strategy: auto } fields: task: type: string length: 256 duedate: type: date manytomany: groups: targetentity: group mappedby: tasks
group.php
<?php namespace appbundle\entity; /** * group */ class group { /** * @var integer */ private $id; /** * @var string */ private $name; /** * 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; } /** * 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 $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); } /** * tasks * * @return \doctrine\common\collections\collection */ public function gettasks() { return $this->tasks; } }
group mapping
appbundle\entity\group: type: entity table: group id: id: type: integer generator: { strategy: auto } fields: name: type: string manytomany: tasks: targetentity: task inversedby: groups cascade: ['persist', 'remove']
the controller
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( )); } }
the exception:
an exception occurred while executing 'insert group (name) values (?)' params ["personal"]: sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near 'group (name) values ('personal')' @ line 1
i think has table constraints, don't know how fix it. i'm pretty sure there's flaw in logic. if can point me right direction quick-fix , explanation/link article why works does, i'll grateful.
from symfony documentation http://symfony.com/doc/current/book/doctrine.html#add-mapping-information
be careful class name , properties aren't mapped protected sql keyword (such group or user). example, if entity class name group, then, default, table name group, cause sql error in engines. see doctrine's reserved sql keywords documentation on how escape these names. alternatively, if you're free choose database schema, map different table name or column name. see doctrine's creating classes database , property mapping documentation.
check doctrine documentation advice on how quote these reserved words: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words
but after adviced not use reserved words in class or class attribute names.
Comments
Post a Comment