1

これを2時間理解しようとしてきましたが、何が悪かったのか理解できません。

Symfony2 と FOSUserBundle を使用しています。

FOSUserBundle の BaseUser クラスを拡張する User エンティティを作成しました。この User エンティティ内には、id、my_mentors、my_mentees の 3 つの変数があります。詳細は以下のとおりです。

class User extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;                

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="my_mentees")
 */
protected $my_mentors;

/**
 * @ORM\ManyToMany(targetEntity="User", inversedBy="my_mentors")
 * @ORM\JoinTable(name="mentor_and_mentee_relationship",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="mentors_or_mentees_user_id", referencedColumnName="id")}
 *      )
 */
protected $my_mentees;

public function __construct()
{
    parent::__construct();
    $this->my_mentors = new ArrayCollection();
    $this->my_mentees = new ArrayCollection();
}

public function __toString()
{
    return $this->getUsername();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}    

/**
 * Add my_mentors
 *
 * @param Fitness\FitBundle\Entity\User $myMentors
 */
public function addUser(\Fitness\FitBundle\Entity\User $myMentors)
{
    $this->my_mentors[] = $myMentors;
}

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

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

メンティー (ユーザー) がメンター (ユーザーでもある) をサブスクライブするため、自己参照を作成しました。次の関数を使用してこれを実行しようとしました。

public function subscribeAction($menteeID, $mentorID)
{                
    $em = $this->getDoctrine()
               ->getEntityManager();

    $mentor = $em->getRepository('TestBundle:User')
                ->find($mentorID);
    $mentee = $em->getRepository('TestBundle:User')
                ->find($menteeID);                

    $currentMentors = $mentee->getMyMentors();
    if ($currentMentors->contains($mentor))
        $this->get('session')->setFlash('subscribe-notice', 'You have already signed up to this mentor!');
    else
    {
        $mentee->setIsMentor(false);
        $mentee->addUser($mentor);
        $mentor->addUser($mentee);
        $em->persist($mentee);
        $em->persist($mentor);
        $em->flush();            
        $this->get('session')->setFlash('subscribe-notice', 'Subscription succesful!');
    }

    return $this->redirect($this->generateUrl('TestBundle_testpage', array('id' => $mentor->getMentorProfile()->getId()) ));
}

ここでの問題は、データベースをチェックすると、データが保持されないことです。メンターとメンティーの関係情報は、アノテーションで宣言されているように、テーブル「mentor_and_mentee_relationship」に格納されません。

$mentor と $mentee の両方を機能させようとして永続化しましたが、どうやら機能しません。

私の ORM アノテーションが間違って宣言される可能性はありますか?

4

1 に答える 1

0

メンターの追加とメンティーの追加に同じ関数 (addUser) を使用しています。これは間違っています。まず、エンティティに 2 つの異なるセッターが必要です (明確にするために addUser の名前を変更しました)。

/**
 * Add my_mentors
 *
 * @param Fitness\FitBundle\Entity\User $myMentors
 */
public function addMentor(\Fitness\FitBundle\Entity\User $myMentors)
{
    $this->my_mentors[] = $myMentors;
}

/**
 * Add my_mentees
 *
 * @param Fitness\FitBundle\Entity\User $myMentees
 */
public function addMentee(\Fitness\FitBundle\Entity\User $myMentees)
{
    $this->my_mentees[] = $myMentees;
}

次に、コントローラーで次のようにします。

        $mentee->addMentor($mentor);
        $mentor->addMentee($mentee);
于 2012-08-22T07:53:23.590 に答える