1

我正在尝试按照本教程将连接表作为表单实体建立多对多关系:http: //www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

就我而言,我有以下课程:

  • 问题/用户/已分配用户

AssignedUser 实体是问题和用户之间的连接表。

以我的形式:

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

我的问题课。省略了一些部分,如命名空间。

(...)

        /**
         * 
         * @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;
    }

问题在于 setAssigned 方法。

   $au->setIssue($this);

我的问题分配用户:

/**
 * 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;

我得到错误:

实体类型 ... IssueAssignedUser 通过外部实体问题具有身份,但是该实体本身没有身份。您必须在相关实体上调用 EntityManager#persist() 并确保在尝试持久化之前生成了标识符

是不是假设学说首先坚持问题实体,只有他们试图坚持相关实体?

我究竟做错了什么?

4

1 回答 1

1

在调用问题之前,您必须将问题持久化到数据库setAssignedUsers()中。

$em->persist($issue);
于 2013-02-20T19:35:40.473 回答