symfony2 - Symfony 2 form associated entity with relation to the parent fails to save -
i trying follow tutorial have many many relation bring together table form entity: http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html
in case have next classes:
issue / user / assigneduserthe assigneduser entity bring together table between issue , user.
in form:
$builder->add('assigned', 'entity', array( 'required' => false, 'class' => 'mybundle:user', 'expanded' => true, 'multiple' => true ));
my issue class. omited parts namespaces.
(...)
/** * * @orm\onetomany(targetentity="(...)assigneduser", mappedby="issue",cascade={"persist", "remove"}) * */ protected $assignedusers; /** * needed form renders users select. */ protected $assigned; public function getassigned() { $assigned = new arraycollection(); foreach($this->assignedusers $value) { $assigned[] = $value->getuser(); } homecoming $assigned; } // of import public function setassigned($users) { foreach($users $user) { $au = new issueassigneduser(); $au->setissue($this); $au->setuser($user); $this->addassigneduser($au); } } /** * build */ public function __construct(){ $this->assignedusers = new arraycollection(); $this->assigned = new arraycollection(); } public function addassigneduser($assigneduser) { $this->assignedusers[] = $assigneduser; homecoming $this; }
the problem lies setassigned method.
$au->setissue($this);
my issueassigneduser:
/** * issueassigneduser * * @orm\table(name="sup_issue_assigned_user") * @orm\entity(repositoryclass="ueb\support\bundle\issuebundle\entity\repository\issueassigneduserrepository") */ class issueassigneduser { /** * @var integer * * @orm\manytoone(targetentity="...\issue",inversedby="assignedusers",cascade={"persist"}) * @orm\joincolumn(name="issue_id", referencedcolumnname="id",nullable=false,ondelete="cascade") * @orm\id */ private $issue; /** * @var \ueb\accounts\bundle\userbundle\entity\user * * @orm\manytoone(targetentity="...\user") * @orm\joincolumn(name="user_id", referencedcolumnname="id",nullable=false,ondelete="cascade") * @orm\id */ private $user; /** * @var \datetime * * @orm\column(name="created_at", type="datetime") * @gedmo\timestampable(on="create") */ private $createdat; /** * @var \datetime * * @orm\column(name="updated_at", type="datetime") * @gedmo\timestampable */ private $updatedat;
i error:
entity of type ... issueassigneduser has identity through foreign entity issue, entity has no identity itself. have phone call entitymanager#persist() on related entity , create sure identifier generated before trying persist
is not suposed doctrine persist issue entity first , them seek persist associated entities?
what doing wrong?
you have persist issue database before calling setassignedusers()
on issue.
$em->persist($issue);
forms symfony2 doctrine2 associations
No comments:
Post a Comment