to many2many selvrefererende samling

stemmer
3

Jeg bruker FOSUserBundle og i min bruker klassen jeg ønsker to ting:

  1. en bruker kan invitere en annen bruker for vennskap. Vennen må bekrefte at vennskap. Derfor en bruker har to attributter $inviteeForFriends, og $invitedFriendssom er en many2many selvreferansekrets for å styre de ventende venn

  2. hvis en venn er bekreftet at den er fjernet fra den pendingfriendship tilkn og tilsatt til venn tilkn: $myFriendsog $friendsWithMesom er en many2many selvreferanse tilkn, også.

Nå i en kontroller gjør jeg dette:

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);
    }
}

Den $user->getInviteeForFriends()->contains($friend)i sluttresultatet i denne feilmeldingen:

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

En echo get_class($user->getInviteeForFriends())ekko en Doctrine\ORM\PersistentCollection. A echo $user->getInviteeForFriends()->count();, oppnås samme Ikke oppgitt indeksfeil.

Disse utsagnene fungerer:

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

men $user->getInviteeForFriends()->contains($friend)gjør det ikke. Hvorfor? Hva gjør jeg galt?

Dette er en del av bruker klasse:

/**
 * @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;
}
}
Publisert på 12/06/2011 klokken 00:04
kilden bruker
På andre språk...                            


1 svar

stemmer
2

I merknadene $inviteeForFriendsskal være like inviteeForFriends. Ingen dollar symbol der. Det er aldri i merknader.

Se også noen eksempler fra læren orm 2,0 dokumentasjon :

/** @Entity */
class Feature
{
    // ...
    /**
     * @ManyToOne(targetEntity="Product", inversedBy="features")
     * @JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product;
    // ...
}
Svarte 12/06/2011 kl. 16:24
kilden bruker

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more