簡體   English   中英

如何定義這種多對多的教義關聯?

[英]How should this Many-To-Many doctrine2 association be defined?

我有兩個實體 - 用戶和挑戰。 用戶可以參與許多挑戰,挑戰可以有許多參與者(用戶)。 我開始通過在Users類上創建“多對多”關系來解決此問題:

/**
     * @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個參與課程之間的多對多對多關系。“

所以我的問題是用戶,挑戰和用戶挑戰之間的這些關聯應該是什么?

UPDATE

有關實體代碼的鏈接,請參閱第一個答案的注釋。 我有一個控制器方法,它總是創建一個新的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(多對一) - >用戶

用戶挑戰(多對一) - >挑戰

挑戰(一對多) - >用戶挑戰

我相信你想要$em->persist($userChallenge); $em->flush();

$userChallenge->setDistance( $userChallenge->getDistance() + (int) $distance );
$em->persist($userChallenge);
$em->flush();

但是我不確定它是否能解決你的問題。

你可以發布isEmpty並在existingChallenges contains函數

這段代碼有效。 一個問題是我正在測試UserChallenge集合是否包含一個顯然會失敗的Challenge對象。

工作代碼:

if ( !$existingChallenges->isEmpty() ) {

          foreach($existingChallenges as $ec) {

           $existingChallengeIdArray[] = $ec->getId();
          }
        }

        if( in_array($challenge_id, $existingChallengeIdArray) ) {


          $userChallenge = $em->getRepository('GymloopCoreBundle:UsersChallenges')
                              ->findOneByChallenge($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($newUserChallenge);
          $em->flush();

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM