2017-01-01 12 views
-1

もう一度、私はできる限り私の質問を簡素化しようとします。私はあまりにもPostRatingに投稿をマップしようとした場合、私は唯一の、各ポストに追加する評価を必要とするのでSymfony doctrine ManyToMany単方向マッピング

まず第一に、私は2つのエンティティ

Post 
PostRating 

を持っている私は、それらの間の一方向の多対多の関係を作成しました循環参照エラーが表示されます。

ポストエンティティ、それが格付けコレクションは、各ポストに追加され、それは同様に期待workes、第三のテーブルpost_has_rating、PostRatingエンティティ内部にマッピングを作成しますが、私はその後、1つの評価を見つけ、必要に応じてそれを編集したい場合はそれは予想よりも大きな頭痛になる。

/** 
* Post have many PostRating 
* @ORM\ManyToMany(targetEntity="PostRating") 
* @ORM\JoinTable(name="post_has_rating", 
*  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="postrating_id", referencedColumnName="id", unique=true)} 
*  ) 
*/ 
protected $ratings; 

のPostController thumbAction、簡単な単語 "ratingAction"

/** 
* Search related videos from youtube 
* @Route("/post/thumb", name="post_thumb") 
* @param Request $request 
* @return string 
*/ 
public function thumbAction (Request $request) { 
    $content = json_decode($request->getContent()); 
    $serializer = $this->get('serializer'); 
    $em = $this->getDoctrine()->getManager(); 

    $postRatingRepo = $this->getDoctrine()->getRepository(PostRating::class); 
    $postRepo = $this->getDoctrine()->getRepository(Post::class); 
    $me = $this->getUser()->getId(); 

    /** @var PostRating $rating */ 
    $rating = $postRatingRepo->findOneBy(['userId' => $me]); 

    /** @var Post $post */ 
    $post = $postRepo->find($content->id); 

    if ($post->getRatings()->contains($rating)) { 
     $post->removeRating($rating); 
     $em->remove($rating); 
    } 

    $rating = new PostRating(); 
    $rating->setUserId($me); 

    switch ($content->action) { 
    //NVM about those hardcoded words, they are about to be removed 
     case 'up': 
       $rating->setRating(1); 
      break; 
     case 'down': 
       $rating->setRating(0); 
      break; 
    } 

    $post->addRating($rating); 

     $em->persist($rating); 
     $em->persist($post); 
     $em->flush(); 

    return new JsonResponse($serializer->normalize(['success' => 'Post thumbs up created'])); 
} 

問題:$rating = $postRatingRepo->findOneBy(['userId' => $me]);この行はすべてraitingsを取得イム今、$post->getRatings()->contains($rating)すぎpostIdを持っている必要があります、それは私が今までに作成したものですが、それを追加するとエラーが発生します。未知の列

私はカスタムリポジトリを作成しなければならないので、DQLで「findRating」のようなものを作成できますか?

OR

私は

+0

でそれを宣言することを忘れ。それ以外にも、DQLまたはクエリービルダーのいずれかを使用して独自のクエリーをリポジトリに書き込むことができます。 –

+0

ありがとう、ちょうど1対多数を使用して、私が正しく覚えていれば、私はすでにそれを試して、循環参照エラーを持っています。 –

+0

@FrankBこれは実際にOneToMany単方向関係を宣言する方法です。注釈OneToManyは双方向関係でのみ使用されます。 – OlivierC

答えて

0
それを使用してのポイントが表示されていないので、私はお互いにもっと簡単な方法にマッピングされたポストとPostRatingエンティティを作ることができ、私は本当に、多対多の関係を望んでいませんあなたがここに一方向のOneToManyおきたい考慮

はヨーヨーに続いて、あなたのポストのエンティティ

namespace AppBundle\Repository; 

use Doctrine\ORM\EntityRepository; 

class PostRepository extends EntityRepository 
{ 
    public function findOneRatingByUser($post, $user) 
    { 
     $query = $this->createQueryBuilder('p') 
      ->select('r') 
      ->innerJoin('p.ratings', 'r') 
      ->where('p.id = :post') 
      ->andWhere('r.user = :user') 
      ->setParameter('post', $post) 
      ->setParameter('user', $user) 
      ->getQuery() 
     ; 

     return $query->getOneOrNullResult(); 
    } 
} 

ためのカスタムリポジトリを作成する私の提案

ですウルコントローラ:カスタムリポジトリが仕事をしたい場合は

public function thumbAction (Request $request) 
{ 
    $content = json_decode($request->getContent()); 
    $serializer = $this->get('serializer'); 
    $em = $this->getDoctrine()->getManager(); 

    $postRepo = $this->getDoctrine()->getRepository(Post::class); 
    $me = $this->getUser()->getId(); 

    /** @var Post $post */ 
    $post = $postRepo->find($content->id); 

    $rating = $postRepo->findOneRatingByUser($post->getId(), $me); 

    if (null === $rating) { 
     $rating = new PostRating(); 
     $rating->setUserId($me); 
    } 

    switch ($content->action) { 
    //NVM about those hardcoded words, they are about to be removed 
     case 'up': 
       $rating->setRating(1); 
      break; 
     case 'down': 
       $rating->setRating(0); 
      break; 
    } 

    $post->addRating($rating); 

     $em->persist($rating); 
     $em->persist($post); 
     $em->flush(); 

    return new JsonResponse($serializer->normalize(['success' => 'Post thumbs up created'])); 
} 

はいけない、私は評価は、唯一つのポストからであるので、oneToMany関係が十分でなければならないことを推測エンティティ

/** 
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository") 
*/ 
class Post 
+0

@KarliOts問題が解決しましたか? – OlivierC

関連する問題