请求symfony中的链接字段 [英] Request a linked field in symfony

查看:64
本文介绍了请求symfony中的链接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,显示有关电影的信息。我在GET中恢复了电影的ID。我想做的是检索每部电影的评论(我的表中有一个filmId列,与电影表的主要ID链接)

I have a page that displays information about a movie. I recover in GET the id of the film. What I would like to do is retrieve the comments for each film (there is a filmId column in my table linked to the primary id of the film table)

   /**
     * @Route("/user/film/{id}", name="film")
     */
    public function film(FilmRepository $repo, CommentRepository $comRepo, EntityManagerInterface $em, Request $req, $id)
    {
        $film = $repo->find($id);

        $comments = $comRepo->findBy(array('id' => $id));





        return $this->render('film/film.html.twig', [
            'controller_name' => 'FilmController',
            'film' => $film,
            'comments' => $comments
        ]);
    }

当我发表 $ comments = $ comRepo-> findBy( array('id'=> $ id)); 我得到一些评论,但是基于他们的id和 NOT 的电影ID(ID为1的评论将显示在电影上ID为1的影片,但例如ID为4且filmId为1的评论不会出现在电影1上,而是出现在ID为4的影片上)

when I make a $comments = $comRepo->findBy(array('id' => $id)); I get some comments, but based on their id and NOT the film id (the comment with id 1 will be displayed on the film with id 1, but for example a comment with id 4 and the filmId a 1 will not appear on film 1, but on the film with id 4)

通过简单地使 $ comments = $ comRepo-> findBy(array('filmId'=> $ id)); 即可访问filmId字段,但是我得到了错误:

I tried to access the filmId field by simply making a $comments = $comRepo->findBy(array ('filmId' => $ id)); but i get the error :

执行'SELECT t0.id AS id_1,t0.content AS content_2,t0.created_at AS created_at_3,t0.author_id AS author_id_4 FROM comment t0 WHERE comment_film.film_id时发生异常=?',带有参数[ 1]:
SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'comment_film.film_id'

An exception occurred while executing 'SELECT t0.id AS id_1, t0.content AS content_2, t0.created_at AS created_at_3, t0.author_id AS author_id_4 FROM comment t0 WHERE comment_film.film_id = ?' with params ["1"]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment_film.film_id' in 'where clause'

我在我的评论存储库中尝试了个性化请求:

I tried a personalized request with, in my Comment repository:


public function findAllWithFilmId($filmId) 
    {
        $em = $this->getEntityManager();

        $query = $em->createQuery(
            'SELECT c
            FROM App\Entity\Comment c
            WHERE c.filmId = :filmId'
        )->setParameter('filmId', $filmId);

        return $query->getResult();
    }

但这似乎无效。.

我在哪里去做这样的请求?
如何修改请求,这似乎是错误的,来自symfony ,而不会破坏一切?还是有更好的方法来解决该问题?

Where do I go to make a request like this ? How to modify the request, which seems erroneous, from symfony without disorganizing everything? or is there a better method to correct the problem?

这是我的评论实体

<?php

namespace App\Entity;

use App\Entity\Film;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CommentRepository")
 */
class Comment
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="comments")
     * @ORM\JoinColumn(nullable=false)
     */
    private $author;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Film", inversedBy="comments")
     * @ORM\JoinColumn(nullable=false)
     */
    private $filmId;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;



    public function getId(): ?int
    {
        return $this->id;
    }

    public function getAuthor(): ?User
    {
        return $this->author;
    }

    public function setAuthor(?User $author): self
    {
        $this->author = $author;

        return $this;
    }


    public function getFilmId(): ?Film
    {
        return $this->filmId;
    }

    public function setFilmId(?Film $filmId): self 
    {
        $this->filmId = $filmId;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }
}

我认为错误可能来自 annotations ,因为我在make:实体中从symfony开始,所以我定义了类型关系,后来在phpmyadmin中更正了,但没有定义代码。例如,我们可以看到filmId在ManyToMany中,但我认为它应该在OneToOne中(FilmId只能有一个id,而一个id只能对应一个filmId),但是我担心如果我更改某些内容,它破坏一切

I think it is possible that the error comes from annotations, because starting on symfony during the make: entity, I defined types relations which I corrected later in phpmyadmin, but not the code. For example we can see that filmId is in ManyToMany, but I think it should be in OneToOne (FilmId can only have one id and an id can only correspond to one filmId), but I'm afraid that if I change certain things it breaks everything.

推荐答案

如果您正确设置了ORM关系,它应该像:

If you have set up your ORM relations correctly, it should be as simple as:

$film = $repo->find($id);
$comments = $film->getComments();

您可能在Film.php中缺少映射。

You might be missing a mapping in Film.php.

这是一个XML示例,应该足够容易转换为注释:

Here's an XML example, should be easy enough to convert to annotations:

电影中:

<one-to-many field="comments" target-entity="App\...\Comments" mapped-by="film"/>

在评论中:

<many-to-one field="film" target-entity="App\...\Film" inversed-by="comments"/>

这篇关于请求symfony中的链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆