brpaz
brpaz

Reputation: 3658

Symfony 2 form associated entity with relation to the parent fails to save

I am trying to follow this tutorial to have a many to many relation with join table as a form entity: http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

In my case I have the following classes:

The AssignedUser entity is the join table between Issue and User.

In my form:

 $builder->add('assigned', 'entity', array(
            'required'  => false,
            'class'     => 'MyBundle:User',
            'expanded' => true,
            'multiple' => true
        ));

My issue class. Omited some parts like namespaces.

(...)

        /**
         * 
         * @ORM\OneToMany(targetEntity="(...)AssignedUser", mappedBy="issue",cascade={"persist", "remove"})
         *
         */
        protected $assignedUsers;

       /**
        * needed for the form to renders the users for select.
        */
        protected $assigned;


        public function getAssigned()
        {
            $assigned = new ArrayCollection();

            foreach($this->assignedUsers as $value)
            {
                $assigned[] = $value->getUser();
            }

            return $assigned;
        }


        // Important
        public function setAssigned($users)
        {
            foreach($users as $user)
            {
                $au = new IssueAssignedUser();

                $au->setIssue($this);
                $au->setUser($user);

                $this->addAssignedUser($au);
            }

        }

        /**
         * construct
         */
        public function __construct(){      
            $this->assignedUsers    = new ArrayCollection();      

            $this->assigned = new ArrayCollection();
}


 public function addAssignedUser($assignedUser)
    {
        $this->assignedUsers[] = $assignedUser;

        return $this;
    }

The problem lies with 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 get the error:

Entity of type ... IssueAssignedUser has identity through a foreign entity Issue, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist

Is not suposed that doctrine persist the Issue entity first and only them try to persist the associated entities?

What am I doing wrong?

Upvotes: 1

Views: 1445

Answers (1)

Bram Gerritsen
Bram Gerritsen

Reputation: 7238

You have to persist the issue to the database before calling setAssignedUsers() on the issue.

$em->persist($issue);

Upvotes: 1

Related Questions