我在另一個鏈接的 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 中為任命_秀所做的
{% 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()”都不存在并且在類“App\”中具有公共訪問權限物體\約會”。
你能幫我嗎?
uj5u.com熱心網友回復:
謝謝,我將代碼更改為以下內容并且它有效:
{% for appointment in appointments %}
{% set cat = appointment.cat %}
<div>
{{ cat.id }}
</div>
{% endfor %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345189.html
上一篇:無法更新作曲家
