【问题标题】:Symfony - Key "skills" for array with keys "0, 1" does not existSymfony - 具有键“0、1”的数组的键“技能”不存在
【发布时间】: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 不出现。 你能帮帮我吗?

【问题讨论】:

    标签: php arrays symfony


    【解决方案1】:

    你把这行的输出转储了吗?

    $jointure = $JointureRepository->findAll();
    

    如果您这样做了,您应该已经看到 $jointure 不是该类的单个对象,而是一个数组。

    您甚至将它用作控制器代码中的数组:

    foreach($jointure as $jointures){
        $skills = $jointures->getSkills();
        $answers = $jointures->getAnswers();
    }
    

    您正在使用数组,因为它将是树枝中的单个对象,这是您的问题。您需要通过阵列获得技能,或者您需要获得单个关节才能随意使用它。

    {% for object in jointure %}
        {% for jointures in object.skills %}   
            {{jointures.label}}   
        {% endfor %}
    {% endfor %}
    

    作为基本注意事项,请查看您对变量的命名。这些具有误导性,并不代表它所包含的价值。

    $arrJointures = $JointureRepository->findAll();
    

    $arrJointures 将是一个合适的变量,因为它包含一个关节数组。此外,使用此命名,您应该会看到此代码无法正常工作。

    {% for jointures in arrJointures.skills %}   
       {{jointures.label}}   
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多