表格收集问题Symfony 5



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

后实体

<?php


/**
* @ORMEntity(repositoryClass=PostRepository::class)
*/
class Post
{
public const PUBLISHED = 1;
public const DRAFT = 0;
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=1000)
*/
private $title;

/**
* @ORMColumn(type="datetime")
*/
private $year;

/**
* @ORMOneToMany(targetEntity="Creator", mappedBy="post", cascade={"persist"})
*/
private $creator;

/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $update_at;

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

创建者实体

<?php

namespace AppEntity;

use AppRepositoryCreatorRepository;
use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass=CreatorRepository::class)
*/
class Creator
{
public const PUBLISHED= 1;
public const DRAFT = 0;

/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $user;

/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $name;

/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $surname;


/**
* @ORMColumn(type="string", length=255)
*/
private $participation;

/**
* @ORMColumn(type="boolean", nullable=true)
*/
private $is_published;

/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $create_at;

/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $update_at;


/**
* @ORMManyToOne(targetEntity="AppEntityPost", inversedBy="creator")
* @ORMJoinColumn(name="post_id", referencedColumnName="id")
*/
private $post;


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


public function getUser(): ?string
{
return $this->user;
}

public function setUser(string $user): self
{
$this->user = $user;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getSurname(): ?string
{
return $this->surname;
}

public function setSurname(?string $surname): self
{
$this->surname = $surname;

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;
}

public function getCreateAt(): ?DateTimeInterface
{
return $this->create_at;
}

public function setCreateAtValue()
{
$this->create_at = new DateTime();
}

public function setCreateAt(DateTimeInterface $create_at): self
{
$this->create_at = $create_at;

return $this;
}

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 getParticipation(): ?string
{
return $this->participation;
}

public function setParticipation(string $participation): self
{
$this->participation = $participation;

return $this;
}

/**
* @return Post
*/
public function getPost()
{
return $this->post;
}

/**
* @param Post $post
* @return Creator
*/
public function setPost(Post $post = null)
{
$this->post = $post;
return $this;
}

/**
* @param Post $post
*/
public function removePost(Post $post)
{
$this->post->removeElement($post);
}
}

后控制器:

/**
* @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', $forRender);
}

Twig:

<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>

由于某种原因,我在数据库中的数据在最后一列中粘在了一起。如何将数据放在正确的位置。下方的屏幕

https://snipboard.io/MpzZHO.jpg

https://snipboard.io/0Y5Cyh.jpg

首先,如果实体是相关的,则可以从post中获取创建者,因此无需在控制器中调用getAllCreator。一旦你有了你的帖子,你在树枝上所做的一切都是为了获得创作者

{{ post.creators }}

{% for creator in post.creators %}

现在,你确定你想让一个帖子有很多创作者吗(帖子上的OneToMany关系(?你能展示一下预期的结果吗?只需根据您提供的屏幕截图写一两行示例即可。

也试试这个方法

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

最新更新