symfony doctrine arrayCollection返回1项

时间:2016-12-06 12:49:20

标签: symfony doctrine

嗨我有参与者的问题我只有1个参与者,但必须更多。

集团实体:

/**
 * One Group has Many participants.
 * @ORM\OneToMany(targetEntity="UserActivity", mappedBy="group")
 */
private $participants;


public function __construct() {
    $this->participants = new ArrayCollection();
}

UserActivity实体:

    /**
     * @var User $user
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="UserActivity")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     */
    private $user;


    /**
     * @var Group $group
     *
     * @ORM\ManyToOne(targetEntity="InformalGroup", inversedBy="UserActivity")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id", nullable=false)
     */
    private $group;


/**
     * @var \DateTime
     *
     * @ORM\Column(name="accepted", type="datetime")
     */
    private $accepted;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="deleted", type="datetime", nullable=true)
     */
    private $deleted;

用户实体:

      /**
     * @var ArrayCollection $userActivity
     *
     * @ORM\OneToMany(targetEntity="UserActivity", mappedBy="user")
     */
    private $userActivity;

  public function __construct() {
        $this->userActivity = new ArrayCollection();
    }

我不知道如何做到这一点我可以按ID分组,并且在这个小组中让参与者获得所有在UserActivity中有关系的人。

1 个答案:

答案 0 :(得分:0)

问题来自注释,mappedByinversedBy属性是错误的,它们必须以实体属性为目标(因此需要提供相同的名称)。

集团实体:

// AppBundle\Entity\InformalGroup.php

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserActivity", mappedBy="group")
 */
private $participants;

UserActivity实体:

// AppBundle\Entity\UserActivity.php

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\InformalGroup", inversedBy="participants")
 * @ORM\JoinColumn(name="group_id", referencedColumnName="id", nullable=false)
 */
private $group;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="userActivity")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
 */
private $user;

用户实体:

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserActivity", mappedBy="user")
 */
private $userActivity;

您可以在docs中深入了解教义注释。