【问题标题】:two many2many self-referencing collection两个many2many自引用集合
【发布时间】:2011-09-13 05:46:12
【问题描述】:

我使用 FOSUserBundle,在我的 User 类中我想要两件事:

  1. 一个用户可以邀请另一个用户建立友谊。朋友必须确认这种友谊。因此一个用户有两个属性$inviteeForFriends$invitedFriends,这是一个many2many自引用关联来管理待处理的友谊

  2. 如果友谊得到确认,它会从待处理的友谊关联中删除并添加到友谊关联中:$myFriends$friendsWithMe,这也是一个 many2many 自引用关联。

现在在控制器中我这样做:

public function addAction() {
    $email = trim($this->get('request')->request->get("email"));
    // ...

    // find requested friend
    $userManager = $this->get('fos_user.user_manager');
    $friend = $userManager->findUserByEmail($email);
    // ...
    $user = $this->container->get('security.context')->getToken()->getUser();

    // ...

    // friend has not already asked the user
    if ($user->getInviteeForFriends()->contains($friend)) {
            throw new Exception(self::FRIEND_HAS_ALREADY_ASKED_MSG, self::FRIEND_HAS_ALREADY_ASKED);
    }
}

$user->getInviteeForFriends()->contains($friend)最终导致这个错误:

Notice: Undefined index: $invitedFriends in /home/r/workspace/MyProject/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 764

echo get_class($user->getInviteeForFriends())Doctrine\ORM\PersistentCollection 相呼应。 echo $user->getInviteeForFriends()->count(); 会导致相同的未定义索引错误。

这些语句有效:

$user->getMyFriends()->contains($friend)
$user->getInvitedFriends()->contains($friend)

$user->getInviteeForFriends()->contains($friend) 没有。为什么?我做错了什么?

这是 User 类的一部分:

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="myFriends")
 */
protected $friendsWithMe;

/**
 * @ORM\ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
 * @ORM\JoinTable(name="friends",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="friend_user_id", referencedColumnName="id")}
 * )
 */
protected $myFriends;


/**
 * I am invited for a friendship from these friends.
 * 
 * @ORM\ManyToMany(targetEntity="User", mappedBy="$invitedFriends")
 */
protected $inviteeForFriends;

/**
 * I invited those friends for a friendship.
 * 
 * @ORM\ManyToMany(targetEntity="User", inversedBy="$inviteeForFriends")
 * @ORM\JoinTable(name="pending_friendships",
 *     joinColumns={@ORM\JoinColumn(name="inviter_user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="invitee_user_id", referencedColumnName="id")}
 * )
 */
protected $invitedFriends;

public function __construct()
{
    parent::__construct();
    $this->friendsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
    $this->myFriends = new \Doctrine\Common\Collections\ArrayCollection();
    $this->inviteeForFriends = new \Doctrine\Common\Collections\ArrayCollection();
    $this->invitedFriends = new \Doctrine\Common\Collections\ArrayCollection();
}


/**
 * Add friendsWithMe
 *
 * @param MyProject\SiteBundle\Entity\User $friendsWithMe
 */
public function addFriendsWithMe(\MyProject\SiteBundle\Entity\User $friendsWithMe)
{
    $this->friendsWithMe[] = $friendsWithMe;
}

/**
 * Get friendsWithMe
 *
 * @return Doctrine\Common\Collections\Collection $friendsWithMe
 */
public function getFriendsWithMe()
{
    return $this->friendsWithMe;
}

/**
 * Add myFriends
 *
 * @param MyProject\SiteBundle\Entity\User $myFriends
 */
public function addMyFriends(\MyProject\SiteBundle\Entity\User $myFriends)
{
    $this->myFriends[] = $myFriends;
}

/**
 * Get myFriends
 *
 * @return Doctrine\Common\Collections\Collection $myFriends
 */
public function getMyFriends()
{
    return $this->myFriends;
}

/**
 * Add inviteeForFriend
 *
 * @param MyProject\SiteBundle\Entity\User $inviteeForFriend
 */
public function addInviteeForFriends(\MyProject\SiteBundle\Entity\User $inviteeForFriend)
{
    $this->inviteeForFriends[] = $inviteeForFriend;
}

/**
 * Get inviteeForFriends
 *
 * @return Doctrine\Common\Collections\Collection $inviteeForFriends
 */
public function getInviteeForFriends()
{
    return $this->inviteeForFriends;
}

/**
 * Add invitedFriend
 *
 * @param MyProject\SiteBundle\Entity\User $invitedFriend
 */
public function addInvitedFriends(\MyProject\SiteBundle\Entity\User $invitedFriend)
{
    $this->invitedFriends[] = $invitedFriend;
}

/**
 * Get invitedFriends
 *
 * @return Doctrine\Common\Collections\Collection $invitedFriends
 */
public function getInvitedFriends()
{
    return $this->invitedFriends;
}
}

【问题讨论】:

  • 注释中的$inviteeForFriends 应该只是inviteeForFriends。那里没有美元符号。

标签: doctrine-orm symfony


【解决方案1】:

注释中的$inviteeForFriends 应该只是inviteeForFriends。那里没有美元符号。它从不在注释中。

另见任何示例from the doctrine orm 2.0 documentation

/** @Entity */
class Feature
{
    // ...
    /**
     * @ManyToOne(targetEntity="Product", inversedBy="features")
     * @JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product;
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多