我如何显示另一个实体链接到主一个在html.twig?



我有一个问题,显示一个实体的属性在另一个的html。链接的小树枝

基本上,一个名为Cats的实体,一个名为约会关系ManyToOne的实体(一只猫可以有几个约会,但一个约会只链接到一只猫)

猫实体:

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

任命实体:

/**
* @ORMManyToOne(targetEntity=Cats::class, inversedBy="appointments", cascade={"persist"} )
* @ORMJoinColumn(nullable=false)
*/
private $cat;
public function getCat(): ?Cats
{
return $this->cat;
}
public function setCat(?Cats $cat): self
{
$this->cat = $cat;
return $this;
}

这是我试图在html中做的。

{% 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()";>>>>>>>>>>>>>>>>>>>

你能帮忙吗?

谢谢你,我把我的代码改成下面的,它工作了:

{% for appointment in appointments %}
{% set cat = appointment.cat %}
<div>
{{ cat.id }}
</div>
{% endfor %}

相关内容

  • 没有找到相关文章

最新更新