简体   繁体   English

Symfony 学说未定义索引

[英]Symfony Doctrine Undefined Index

I'm trying yo make a Doctrine Query by using join queries.我正在尝试使用连接查询来制作 Doctrine 查询。 I have done the following thing :我做了以下事情:

    public function getCategoriesFromCategorieParentAndConstructeur(): ?Array
    {
        return $this->createQueryBuilder('c')
            ->leftJoin('c.categorie_parent_id', 'cp' )
            ->getQuery()
            ->getResult();
    }

But when I try to display the result the following error appears :但是当我尝试显示结果时出现以下错误:

Notice: Undefined index: categories注意:未定义索引:类别

I have no idea why can you tell me more ?我不知道你为什么能告诉我更多?

Here are my 2 Entity : Categorie Entity这是我的 2 实体:类别实体

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
 */
class Categorie
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $categorie_intitule;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\CategorieParent", inversedBy="categorie_id")
     */
    private $categorie_parent_id;


    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="categories")
     */
    private $created_by;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Produit", mappedBy="categorie_id", orphanRemoval=true)
     */
    private $produits;

CategorieParent Entity类别父实体

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Categorie", mappedBy="categorie_parent_id", cascade={"remove"})
     */
    private $categorie_id;

    public function __construct()
    {
        $this->categorie_id = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCategorieIntitule(): ?string
    {
        return $this->categorie_intitule;
    }

    public function setCategorieIntitule(string $categorie_intitule): self
    {
        $this->categorie_intitule = $categorie_intitule;

        return $this;
    }

Tahnk you谢谢你

You could follow what this Notice tells you and define your variable $categories in your User entity, which will probably be:您可以按照此通知告诉您的内容并在您的用户实体中定义您的变量$categories ,这可能是:

/**
 * @ORM\OneToMany(targetEntity=App\Entity\Categorie, mappedBy="created_by")
 */
private $categories;

and this should be the best option, since it will give you a correct mapping between DB and Entities.这应该是最好的选择,因为它将为您提供数据库和实体之间的正确映射。

Otherwise you can just disable notice reporting :否则,您可以禁用通知报告

<?php
    error_reporting(E_ERROR | E_WARNING | E_PARSE); 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM