表格收集问题Symfony 5

表格收集问题Symfony 5,symfony,twig,symfony5,Symfony,Twig,Symfony5,我对symfony 5有些问题。2个实体(帖子和创建者) 邮政实体 <?php /** * @ORM\Entity(repositoryClass=PostRepository::class) */ class Post { public const PUBLISHED = 1; public const DRAFT = 0; /** * @ORM\

我对symfony 5有些问题。2个实体(帖子和创建者)

邮政实体

 <?php
    
         
    /**
     * @ORM\Entity(repositoryClass=PostRepository::class)
     */
    class Post
    {
        public const PUBLISHED = 1;
        public const DRAFT = 0;
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=1000)
         */
        private $title;
    
        /**
         * @ORM\Column(type="datetime")
         */
        private $year;
    
        /**
         * @ORM\OneToMany(targetEntity="Creator", mappedBy="post", cascade={"persist"})
         */
        private $creator;
    

        /**
         * @ORM\Column(type="datetime", nullable=true)
         */
        private $update_at;
    
        /**
         * @ORM\Column(type="boolean")
         */
        private $is_published;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->creator = new ArrayCollection();
        }
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getTitle(): ?string
        {
            return $this->title;
        }
    
        public function setTitle(string $title): self
        {
            $this->title = $title;
    
            return $this;
        }
    
        public function getYear(): ?\DateTimeInterface
        {
            return $this->year;
        }
    
        public function setYear(\DateTimeInterface $year): self
        {
            $this->year = $year;
    
            return $this;
        }
    
       
        /**
         * @param Creator $creator
         * @return Post
         */
        public function addCreator(Creator $creator)
        {
            $this->creator[] = $creator;
            $creator->setPost($this);
            return $this;
        }
    
        /**
         * @param Creator $creator
         */
        public function removeCreator(Creator $creator)
        {
            $this->creator->removeElement($creator);
        }
    
        /**
         * @return Collection
         */
        public function getCreator()
        {
            return $this->creator;
        }
    
        public function getUpdateAt(): ?\DateTimeInterface
        {
            return $this->update_at;
        }
    
        public function setUpdateAtValue()
        {
            $this->update_at = new \DateTime();
        }
    
        public function setUpdateAt(?\DateTimeInterface $update_at): self
        {
            $this->update_at = $update_at;
    
            return $this;
        }
    
        public function getIsPublished(): ?bool
        {
            return $this->is_published;
        }
    
        public function setIsPublished()
        {
            $this->is_published = self::PUBLISHED;
        }
    
        public function setIsDraft()
        {
            $this->is_published = self::DRAFT;
        }
    }
细枝:


阿克贾
蒂图尔
韩国
要点
科菲伦西亚
{check_creator%中的check_creator%为%0}
克托·斯特沃齐
乌兹亚
{%endfor%}
{%用于post%}
{{post_item.title}
{{post_item.year}日期('Y-m-d')}
{{post_item.numOfPoints}}
{{post_item.conference}
{%endfor%}
{check_creator%中的check_creator%为%0}
{{check_creators.user}
{{check_creators.participation}}
{%endfor%}
由于某种原因,数据库中的数据在最后一列中粘在一起。如何将数据放在正确的位置。下面的屏幕


首先,如果实体是相关的,您可以从post获取创建者,所以不需要在controller中调用getAllCreator。一旦你有了你的帖子,你在twig中所做的一切都是为了得到创建者

{{ post.creators }}

现在,你确定你想要一个帖子有多个创作者(帖子上有一对夫妻关系)?你能展示一下预期的结果是什么样的吗?只需根据您提供的屏幕截图编写一两行示例。

也可以尝试这种方法

 /**
     * @Route("/admin/post", name="admin_post")
     */
    public function index()
    {
        $forRender = parent::renderDefault();
        $forRender['title'] = 'Wszystkie publikacje';
        $forRender['post'] = $this->postRepository->getAllPost();
        $forRender['check_creator'] = $this->creatorRepository->getAllCreator();
        return $this->render('admin/post/index.html.twig',
           'post'=>$forRender
        );
    }

是的,终于明白了。将我的{%forcheck_creator%}中的check_creators更改为{%check_creators in post_item.creator%}。2.确切地说,我有许多创作者到一个帖子(许多创作者像我一样有一个文档,但他们的许多创作者像《独立宣言》)
    <table>
        <thead>
        <tr>
            <th>Akcja</th>
            <th>Tytul</th>
            <th>Rok</th>
            <th>Points</th>
            <th>Konferencja</th>
            {% for check_creators in check_creator %}
            <th>Kto stworzył</th>
            <th>Udział</th>
            {% endfor %}
        </tr>
        </thead>
        <tbody>
        {% for post_item in post %}
        <tr>
            <td><a href="{{ path('admin_post_update', {'id': post_item.id}) }}" class="btn-link">Modyfikuj</a></td>
            <td>{{ post_item.title }}</td>
            <td>{{ post_item.year|date('Y-m-d') }}</td>
            <td>{{ post_item.numOfPoints }}</td>
            <td>{{ post_item.conference }}</td>
            {% endfor %}
            {% for check_creators in check_creator %}
            <td>{{ check_creators.user }}</td>
            <td>{{ check_creators.participation }}</td>
            {% endfor %}
        </tr>

        </tbody>
    </table>
{{ post.creators }}
{% for creator in post.creators %}
 /**
     * @Route("/admin/post", name="admin_post")
     */
    public function index()
    {
        $forRender = parent::renderDefault();
        $forRender['title'] = 'Wszystkie publikacje';
        $forRender['post'] = $this->postRepository->getAllPost();
        $forRender['check_creator'] = $this->creatorRepository->getAllCreator();
        return $this->render('admin/post/index.html.twig',
           'post'=>$forRender
        );
    }