2011-10-11 22 views
6

İki Varlığım var - Kullanıcılar & Zorluklar. Bir Kullanıcı birçok zorluğa katılabilir ve bir meydan okuma birçok katılımcıya (kullanıcı) sahip olabilir. Benim Kullanıcılar sınıfı bir Çok-çok ilişki oluşturarak bu sorunu yaklaşırken başladı: AncakBu Çoktan-Çok doktrin2 ilişkisi nasıl tanımlanmalıdır?

/** 
    * @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; 

, ben o zaman ben bir mesafe kullanıcı/meydan kombinasyonu karşı özellik (ne kadar kullanıcı saklamak gerektiğini fark etti meydan okumalarında seyahat etti. Doctrine2 docs durumu:

"Neden çoktan çoğa ilişkilendirmeler daha az yaygındır? Çünkü sık sık bir ilişkilendirmeyle ek öznitelikleri ilişkilendirmek istersiniz, bu durumda bir ilişkilendirme sınıfı eklersiniz. Sonuç olarak, doğrudan Birçok birlik ortadan kalkar ve 3 katılımcı sınıf arasında bire-çok/çoktan bire birlikler tarafından değiştirilir.

Bu yüzden sorum, bu ilişkilerin Kullanıcı, Mücadeleler ve UsersChallenges arasında ne olması gerektiği? Varlık koduna bağlantılar için ilk cevaba

GÜNCELLEME

bakınız yorumu. (> UsersChallenges

UsersChallenges - Hep (istediğim budur)

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 

     } 

    } 

cevap

1

i olmak Eğer bu sizin sorunu çözmeyi

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

$em->flush(); Ancak emin değilim bir $em->persist($userChallenge); önce istediğiniz yaşıyor.

Eğer isEmpty ve bu kod çalıştı existingChallenges

8

Kullanıcı (bire-çok) mevcut bir güncelleme daha yeni UsersChallenges rekor ziyade yaratan altında bir kontrolör yöntemine sahip birçok bire) -> Kullanıcı

UsersChallenges (çok-bir) -> Mücadelesi

Mücadelesi (bire-çok) -> UsersChallenges

+0

teşekkürler. Bunlar sana iyi mi geliyor? https://gist.github.com/1280574, https://gist.github.com/1280576, https://gist.github.com/1280579 – codecowboy

0

yılında contains fonksiyon gönderebilir miyim. Bir sorun, bir UserChallenge koleksiyonunun açık bir şekilde başarısız olan bir Challenge nesnesi içerip içermediğini test etmekti.

Çalışma kodu:

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

     } 
İlgili konular