我有两个实体 - 用户和挑战。一个用户可以参与很多挑战,一个挑战可以有很多参与者(用户)。我开始通过在我的用户类上创建多对多关系来解决这个问题:
/**
* @ORM\ManytoMany(targetEntity="Challenge")
* @ORM\JoinTable(name="users_challenges",joinColumns={@ORM\JoinColumn(name="user_id",referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="challenge_id",referencedColumnName="id")})
*
*/
protected $challenges;
但是,我随后意识到我需要针对用户/挑战组合存储距离属性(用户在他们的挑战中走了多远)。 Doctrine2 文档状态:
“为什么多对多关联不太常见?因为你经常希望将附加属性与关联关联,在这种情况下你会引入一个关联类。因此,直接的多对多关联消失并被取代通过 3 个参与类之间的一对多/多对一关联。”
所以我的问题是 User、Challenge 和 UsersChallenges 之间的这些关联应该是什么?
更新
有关实体代码的链接,请参阅对第一个答案的评论。我在下面有一个 Controller 方法,它总是创建一个新的 UsersChallenges 记录,而不是更新现有记录(这正是我想要的)
public function updateUserDistanceAction()
{
$request = $this->getRequest();
$distance = $request->get('distance');
$challenge_id = $request->get('challenge_id');
if($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getEntityManager();
$user = $this->get('security.context')->getToken()->getUser();
$existingChallenges = $user->getChallenges();
$challengeToUpdate = $em->getRepository('GymloopCoreBundle:Challenge')
->find( (int) $challenge_id);
if(!$challengeToUpdate) {
throw $this->createNotFoundException('No challenge found');
}
//does the challengeToUpdate exist in existingChallenges? If yes, update UsersChallenges with the distance
//if not, create a new USersChallenges object, set distance and flush
if ( !$existingChallenges->isEmpty() && $existingChallenges->contains($challengeToUpdate)) {
$userChallenge = $em->getRepository('GymloopCoreBundle:UsersChallenges')
->findOneByChallengeId($challengeToUpdate->getId());
$userChallenge->setDistance( $userChallenge->getDistance() + (int) $distance );
$em->flush();
} else {
$newUserChallenge = new UsersChallenges();
$newUserChallenge->setDistance($distance);
$newUserChallenge->setChallenge($challengeToUpdate);
$newUserChallenge->setUser($user);
$user->addUsersChallenges($newUserChallenge);
$em->persist($user);
$em->persist($newUserChallenge);
$em->flush();
}
//if success
return new Response('success');
//else
}
}
最佳答案
用户(一对多)-> UsersChallenges
UsersChallenges(多对一)-> 用户
UsersChallenges(多对一)-> 挑战
挑战(一对多)-> 用户挑战
关于php - 应该如何定义这种多对多 doctrine2 关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7722273/