-2

I have this issue and I don't know really why...

The Repository :

    /**
     * @return Creations[]
     */

    public function displayOne(): array
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.category.id = 1')
            ->setMaxResults(3)
            ->getQuery()
            ->getResult();
        // returns an array of Product objects
        return $query->getResult();
    }


}

The Entity :

    /**
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="creations")
     * @ORM\JoinColumn(nullable=false)
     */
    private $category;

The controller function :

    public function index(): Response
    {

        $repository = $this->entityManager->getRepository(Creations::class);
        $creations = $repository->findAll();


        $categories = $this->entityManager->getRepository(Category::class)->findAll();

        return $this->render('creations/index.html.twig', [

            'creations' => $repository->displayOne(),
            'categories' => $categories

        ]);
    }

Also when I dd($creations), I got the good "link" thecreation -> category -> id -> 1 for example

Thanks for your help !

Locc35
  • 1
  • 1

1 Answers1

0

Try this:

public function displayOne(Category $category): array
{
    return $this->createQueryBuilder('c')
        ->andWhere('c.category = :category')
        ->setParameter('category', $category)
        ->setMaxResults(3)
        ->getQuery()
        ->getResult();
}

Or alternatively:

public function displayOne(): array
{
    return $this->createQueryBuilder('c')
        ->join('c.category', 'cat')
        ->andWhere('cat.id = :category')
        ->setParameter('category', 1)
        ->setMaxResults(3)
        ->getQuery()
        ->getResult();
}

The three parts selector c.category.id is just not going to work.

Julien B.
  • 3,023
  • 2
  • 18
  • 33