Symfony-键“ 0、1”的数组的键“技能”不存在

时间:2019-02-27 12:00:51

标签: php arrays symfony

我正在尝试在树枝中显示我的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”的数组的键“技能”不存在。 在线:{%of jointure.skills%}

如果我这样做:

{% for skill in skills %}
{{skill.id}}
{% endfor %}

3,4出现,但与id = 0。的Jointure相关的1,2没有出现。 你能帮我吗?

1 个答案:

答案 0 :(得分:2)

您是否转储了此行的输出?

$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 %} 将是一个合适的变量,因为它包含一组关节。另外,使用这种命名方式,您应该看到该代码无法正常工作。

flex