Reputation: 3
I am trying to display only active adverts in my view, but I get an error:
Unable to guess how to get a Doctrine instance from the request information for parameter "advert".
When I am displaying all adverts, there is no problem:
/**
* @Route("/", name="advert_index")
*
* @return Response
*/
public function indexAction()
{
$entityManager = $this->getDoctrine()->getManager();
$adverts = $entityManager->getRepository(Advert::class)->findAll();
return $this->render("Advert/index.html.twig", ["adverts" => $adverts]);
}
But when I am trying to display only active adverts, I coded:
/**
* @Route("/", name="advert_index")
*
* @param Advert $advert
* @return Response
*/
public function indexAction(Advert $advert)
{
$entityManager = $this->getDoctrine()->getManager();
$adverts = $entityManager->getRepository(Advert::class)->findBy(["status" => $advert->getStatus() === Advert::STATUS_ACTIVE]);
$adverts = $entityManager->getRepository(Advert::class)->findBy([$advert->getStatus() === Advert::STATUS_ACTIVE]);
$adverts = $entityManager->getRepository(Advert::class)->findBy($advert->getStatus() === Advert::STATUS_ACTIVE);
return $this->render("Advert/index.html.twig", ["adverts" => $adverts]);
}
These are the 3 ways which i tried. But it does not work. Can somebody help me?
Upvotes: 0
Views: 2260
Reputation: 17166
Your problem is the method signature for your action:
public function indexAction(Advert $advert)
The Doctrine ParamConverter tries to fetch an advert for you, but since there is no {id}
in the route itself, the converter is not able to identify which advert to load.
Since you don't want one specific advert removing the parameter should be fine:
public function indexAction()
{
...
}
Then you can change your findBy-method to just be:
findBy(['status' => Advert::STATUS_ACTIVE]);
This should give you all active adverts.
Upvotes: 2