0

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

LeJeuneDev
  • 21
  • 8
  • 1
    `$created_by` points to the User entity and is inversed by `categories`. So your User does not have that property. Also if you are using it check the profiler, you might have invalid entities. – Martin M. Aug 28 '20 at 09:38
  • Can you include `App\Entity\User` entity mappings – M Khalid Junaid Aug 30 '20 at 19:33

1 Answers1

-2

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); 
?>
Agnohendrix
  • 480
  • 1
  • 7
  • 17
  • Disable error reporting? Seriously? Plus your disabling approach will not work for this question. Consider updating your 'answer'. Might even consider posting a working answer instead of a guess. – Cerad Aug 28 '20 at 12:52
  • @Cerad My First solution should solve the problem, as Martin M. wrote. But since it's a Notice, things should work disabling error(or just notice) reporting without having side effects. Of course a correct entity mapping is the right way to go. – Agnohendrix Aug 28 '20 at 13:10
  • Still wrong on both counts. Try implementing your solution to see why it won't work. Normally not a down voter but will make an exception in this case. – Cerad Aug 28 '20 at 13:33
  • @Cerad Disable error reporting is just a workaround, not a complete solution, as i wrote. Adding $categories field with mappedBy="created_by" should solve it in the best way. If that's not the case i deserve that downvote and i'll follow this question to understand where i am wrong. – Agnohendrix Aug 28 '20 at 13:43
  • Until OP responds and shares what exactly is needed, we won't know if this is the correct approach. But according to the [doctrine documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/association-mapping.html#one-to-many-bidirectional) simply adding the property in the User entity is not enough. A reference column needs to be set for `$created_by` as it is missing in the original post. Oh yeah, and the ArrayCollection part too is missing. – Martin M. Aug 28 '20 at 14:56
  • Hi all @MartinM.you're solution in commentaries solved my problem, unfortunately I can't mark it as solved. – LeJeuneDev Sep 09 '20 at 09:31