'Problems with rendering the results of searching in Symfony 5.4
I've recently started learning Symfony (from Documentation and Google;) so I have a lot of problems. I want to search for games according to selected attributes (name, category, age, number of players, price, deposit). In SearchController I've created a form and in Game (Gra) Repository I built a QueryBuilder which I am trying to call in SearchController and render to search/result.twig.html I am probably making a lot of mistakes, I try to fix them one by one, the last one is this:
Method name must be a string in this verse: $ Games = $ this -> $ repository (Game :: class) -> findByField ($ Name, $ id_Category, $ Number of players, $ Age_of_years, $ Price, $ Deposit);
Is this because some of the fields are int? But anyway, even if I changed it to string, I can't render it ... There are a lot of people with experience here, so I count on your help :)
MY GRA GAMEREPOSITORY
public function findByExampleField(?string $Nazwa,string $id_Kategoria,string $Liczba_graczy,string $Wiek_od_lat, int $Cena, int $Kaucja)
{
$qb = $this->createQueryBuilder('a')
->andwhere('a.Nazwa = :Nazwa')
->orWhere('a.id_Kategoria = :Kategoria')
->orWhere('a.Liczba_graczy = :Liczba graczy')
->orWhere('a.Wiek_od_lat = :Wiek od lat')
->orWhere('a.Cena = :Cena')
->orWhere('a.Kaucja = :Kaucja')
->setParameter(':Nazwa', $Nazwa)
->setParameter(':Kategoria', $id_Kategoria)
->setParameter(':Liczba graczy', $Liczba_graczy)
->setParameter(':Wiek od lat', $Wiek_od_lat)
->setParameter(':Cena', $Cena)
->setParameter(':Kaucja', $Kaucja);
$query = $qb->getQuery();
return $query->execute();
}
MY SEARCHCONTROLLER
{
#[Route('/search', name: 'app_search')]
public function searchAction(Request $request, GraRepository $repository, string $Nazwa='Nazwa',string $id_Kategoria="Kategoria",string $Liczba_graczy='Liczba_graczy',string $Wiek_od_lat ='Wiek_od_lat', string $Cena = 'Cena', string $Kaucja = 'Kaucja'):Response
{
$form = $this->createFormBuilder([])
->add('Nazwa', EntityType::class, ['required' => false, 'label' => 'Nazwa gry:', 'class' => Gra:: class ] )
->add('id_Kategoria', EntityType::class, ['required' => false, 'label' => 'Kategoria:', 'class' => Kategoria:: class])
->add('Liczba_graczy', EntityType::class, ['required' => false, 'label' => 'Liczba graczy:', 'class' => Gra:: class,'choice_label' => 'Liczba_graczy','expanded' => true, 'multiple'=> false,
'query_builder' => function (GraRepository $repository)
{
return $repository
->createQueryBuilder('l')
->select('l')
->groupBy('l.Liczba_graczy');
} ])
ADDING A FEW MORE FIELDS (...)
} ])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$form = $request->request->get('Nazwa', 'id_Kategoria', 'Liczba_graczy', 'Wiek_od_lat', 'Cena', 'Kaucja');
$Gry=$this->$repository(Gra::class)-> findByField ($Nazwa,$id_Kategoria,$Liczba_graczy,$Wiek_od_lat, $Cena, $Kaucja);
if (!$Gry) {
throw $this->createNotFoundException(
'Taka gra nie istnieje'
);
}
return $this->render('search/result.html.twig', ['Gry' => $Gry]);
}
return $this->render('search/advsearch.html.twig', ['form' => $form->createView()]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Gra::class,
]);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
