如何在 html.twig 上显示链接到主实体的另一个实体?
How can I show another Entity linked to the main one on html.twig?
我无法在链接的另一个 html.twig 上展示一个实体的 属性。
基本上,一个名为 Cats 的实体,一个名为 Appointment 关系的实体 ManyToOne(一只猫可以有多个约会,但一个约会只链接到一只猫)
猫实体:
/**
* @ORM\OneToMany(targetEntity=Appointment::class, mappedBy="cat", orphanRemoval=true)
*/
private $appointments;
/**
* @return Collection|Appointment[]
*/
public function getAppointments(): Collection
{
return $this->appointments;
}
public function addAppointment(Appointment $appointment): self
{
if (!$this->appointments->contains($appointment)) {
$this->appointments[] = $appointment;
$appointment->setCat($this);
}
return $this;
}
public function removeAppointment(Appointment $appointment): self
{
if ($this->appointments->removeElement($appointment)) {
// set the owning side to null (unless already changed)
if ($appointment->getCat() === $this) {
$appointment->setCat(null);
}
}
return $this;
}
预约实体:
/**
* @ORM\ManyToOne(targetEntity=Cats::class, inversedBy="appointments", cascade={"persist"} )
* @ORM\JoinColumn(nullable=false)
*/
private $cat;
public function getCat(): ?Cats
{
return $this->cat;
}
public function setCat(?Cats $cat): self
{
$this->cat = $cat;
return $this;
}
这是我在 html.twig 中为 appointment_show
尝试做的事情
{% extends 'base.html.twig' %}
{% block title %}Appointment{% endblock %}
{% block main %}
<h1>Appointment</h1>
{% for cat in appointment.cats %}
<div>
<td>{{ appointment.cat_id }}</td>
</div>
{% endfor %}
所以我一直收到错误消息:
属性“cats”和“cats()”、“getcats()”/“iscats()”/“hascats()”或“ __call()”在 class“App\Entity\Appointment”中存在并具有 public 访问权限。
你能帮忙吗?
谢谢,我将代码更改为以下并且有效:
{% for appointment in appointments %}
{% set cat = appointment.cat %}
<div>
{{ cat.id }}
</div>
{% endfor %}
我无法在链接的另一个 html.twig 上展示一个实体的 属性。
基本上,一个名为 Cats 的实体,一个名为 Appointment 关系的实体 ManyToOne(一只猫可以有多个约会,但一个约会只链接到一只猫)
猫实体:
/**
* @ORM\OneToMany(targetEntity=Appointment::class, mappedBy="cat", orphanRemoval=true)
*/
private $appointments;
/**
* @return Collection|Appointment[]
*/
public function getAppointments(): Collection
{
return $this->appointments;
}
public function addAppointment(Appointment $appointment): self
{
if (!$this->appointments->contains($appointment)) {
$this->appointments[] = $appointment;
$appointment->setCat($this);
}
return $this;
}
public function removeAppointment(Appointment $appointment): self
{
if ($this->appointments->removeElement($appointment)) {
// set the owning side to null (unless already changed)
if ($appointment->getCat() === $this) {
$appointment->setCat(null);
}
}
return $this;
}
预约实体:
/**
* @ORM\ManyToOne(targetEntity=Cats::class, inversedBy="appointments", cascade={"persist"} )
* @ORM\JoinColumn(nullable=false)
*/
private $cat;
public function getCat(): ?Cats
{
return $this->cat;
}
public function setCat(?Cats $cat): self
{
$this->cat = $cat;
return $this;
}
这是我在 html.twig 中为 appointment_show
尝试做的事情{% extends 'base.html.twig' %}
{% block title %}Appointment{% endblock %}
{% block main %}
<h1>Appointment</h1>
{% for cat in appointment.cats %}
<div>
<td>{{ appointment.cat_id }}</td>
</div>
{% endfor %}
所以我一直收到错误消息:
属性“cats”和“cats()”、“getcats()”/“iscats()”/“hascats()”或“ __call()”在 class“App\Entity\Appointment”中存在并具有 public 访问权限。
你能帮忙吗?
谢谢,我将代码更改为以下并且有效:
{% for appointment in appointments %}
{% set cat = appointment.cat %}
<div>
{{ cat.id }}
</div>
{% endfor %}