【发布时间】:2019-02-27 12:00:51
【问题描述】:
我正在尝试在 twig 中显示我的 Jointure 实体的所有相关属性。
这是我的 3 个实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\JointureRepository")
*/
class Jointure
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Skill", mappedBy="jointure")
*/
private $skills;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Answer", mappedBy="jointure")
*/
private $answers;
public function __construct()
{
$this->skills = new ArrayCollection();
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|Skill[]
*/
public function getSkills(): Collection
{
return $this->skills;
}
public function addSkill(Skill $skill): self
{
if (!$this->skills->contains($skill)) {
$this->skills[] = $skill;
$skill->setJointure($this);
}
return $this;
}
public function removeSkill(Skill $skill): self
{
if ($this->skills->contains($skill)) {
$this->skills->removeElement($skill);
// set the owning side to null (unless already changed)
if ($skill->getJointure() === $this) {
$skill->setJointure(null);
}
}
return $this;
}
/**
* @return Collection|Answer[]
*/
public function getAnswers(): Collection
{
return $this->answers;
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setJointure($this);
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
// set the owning side to null (unless already changed)
if ($answer->getJointure() === $this) {
$answer->setJointure(null);
}
}
return $this;
}
}
ManyToOne => 关节关系的技能实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\SkillRepository")
*/
class Skill
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $label;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Jointure", inversedBy="skills")
* @ORM\JoinColumn(nullable=false)
*/
private $jointure;
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getJointure(): ?Jointure
{
return $this->jointure;
}
public function setJointure(?Jointure $jointure): self
{
$this->jointure = $jointure;
return $this;
}
}
我的实体答案与具有 UserEmail 属性的技能相同。
所以在我的控制器中:
class HomeController extends AbstractController
{
/**
* @var JointureRepository
*/
public function __construct(JointureRepository $repository, ObjectManager $em)
{
$this->repository = $repository;
$this->em = $em;
}
/**
* @Route("/", name="home")
*/
public function index()
{
$JointureRepository = $this->getDoctrine()->getRepository(Jointure::class);
$jointure = $JointureRepository->findAll();
foreach($jointure as $jointures){
$skills = $jointures->getSkills();
$answers = $jointures->getAnswers();
}
$this->em->flush();
return $this->render('pages/home.html.twig', [
'controller_name' => 'HomeController',
'jointure' => $jointure,
'skills' => $skills,
'answers' => $answers,
]);
}
}
当我试图在树枝中显示它们时:
{% for jointures in jointure.skills %}
{{jointures.label}}
{% endfor %}
我有以下错误:键为“0, 1”的数组的键“技能”不存在。 At line : {% for jointures in jointure.skills %}
如果我这样做:
{% for skill in skills %}
{{skill.id}}
{% endfor %}
3,4 出现,但与 id = 0 的 Jointure 相关的 1,2 不出现。 你能帮帮我吗?
【问题讨论】: