Tom Eichelbrenner
Tom Eichelbrenner

Reputation: 40

Putting a limit on doctrine queries

I'm working on a site to make an inventory of series.

public function category(string $categoryName): Response
{
    $categoryInfos = $this->getDoctrine()
        ->getRepository(Category::class)
        ->findOneByName($categoryName);

    $programs = $this->getDoctrine()
        ->getRepository(Program::class)
        ->findByCategory($categoryInfos);

    return $this->render('wild/category.html.twig', ['programs' => $programs, 'category' => $categoryInfos]);
}

This function allows me to retrieve all programs belonging to the specified category. I would now like to limit the number of programs to 3 in my request.

$programs = $this->getDoctrine()
        ->getRepository(Program::class)
        ->findByCategory($categoryInfos)
        ->setMaxResults(3);

But this shows me the error: -> Call to a member function setMaxResults() on array

What did I do wrong? I get lost in the symfony doc, being new ^^

Upvotes: 1

Views: 585

Answers (1)

Amir Etemad
Amir Etemad

Reputation: 106

You are calling a method inside repository class that returns an array and you cannot execute setMaxResults() on arrays.

If you want to set the size of result you should set it inside findByCategory() method in ProgramRepository.php

Upvotes: 1

Related Questions