简体   繁体   中英

Symfony Doctrine Undefined Index

I'm trying yo make a Doctrine Query by using join queries. 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

/**
 * @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:

/**
 * @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); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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