Symfony原则多个单向映射

Symfony原则多个单向映射,symfony,doctrine,Symfony,Doctrine,好吧,再说一遍,我会尽量简化我的问题 首先,我有两个实体 Post PostRating 我已经在它们之间建立了单向的多个关系,因为我只需要将评级添加到每个帖子,如果我也尝试将帖子映射到帖子,我会得到循环引用错误 Post实体,它创建第三个表Post_有_评级,PostRating实体内部没有映射,它的工作方式与预期类似,评级集合添加到每个帖子中,但如果我想找到一个评级,并在需要时对其进行编辑,那么它会比预期的更令人头痛 后置控制器拇指动作,简单的单词“ratingAction” 问题:$ra

好吧,再说一遍,我会尽量简化我的问题

首先,我有两个实体

Post
PostRating
我已经在它们之间建立了单向的多个关系,因为我只需要将评级添加到每个帖子,如果我也尝试将帖子映射到帖子,我会得到循环引用错误

Post实体,它创建第三个表Post_有_评级,PostRating实体内部没有映射,它的工作方式与预期类似,评级集合添加到每个帖子中,但如果我想找到一个评级,并在需要时对其进行编辑,那么它会比预期的更令人头痛

后置控制器拇指动作,简单的单词“ratingAction”

问题:
$rating=$postRatingRepo->findOneBy(['userId'=>$me])
这一行也需要有postId,用于
$post->getRatings()->contains($rating)
,现在我正在获取我曾经创建的所有衣服,但是如果我添加它,它会抛出错误,未知列

我是否应该创建自定义存储库,以便使用DQL创建类似“findRating”的内容


我可以用一种更简单的方式将Post和PostRating实体映射到彼此之间吗?我真的不想要多对多的关系,因为我不认为使用它有什么意义

为您的Post实体创建自定义存储库

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'] ) );
}
如果希望自定义存储库正常工作,请不要忘记在实体中声明它

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

我想,一个一对一的关系应该足够了,因为评级只能来自一个帖子。除此之外,您还可以使用DQL或查询生成器在存储库中编写自己的查询。谢谢,我只使用一对多,如果我没记错的话,我已经尝试过了,但出现了循环引用错误。@FrankB这实际上是您声明一对多单向关系的方式。注释OneToMany仅用于双向关系。@KarliOts在列命名时要小心,这里您的用户id列实际上包含一个帖子id,而不是用户id。您能更清楚地说明您想做什么吗?@OlivierC嗯,我想做的是post->有很多喜欢的东西,向上和向下,你怎么知道用户id包含post\u id?我也发现了这一点,我怎样才能做到,所以我在那里命名了post_id,因为如果我把它改成post_id,然后按照用户的id进行搜索,不知道怎么可能,如果你想的话,我可以提供更多的代码。
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'] ) );
}
/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post