Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Symfony problem z wyszukiwaniem
paprikano
post 6.05.2018, 11:40:23
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 6.05.2018

Ostrzeżenie: (0%)
-----


Witam, mam mały problem. Mianowicie mam encje Advert z ogloszeniami, jest poalczona relacja jeden do wielu z encja Address, natomiast Address tez jest relacja jeden do wielu z encja City.
Probuje zrobic wyszukiwarke, w zmiennejj $selectedCity dobrze mi pobiera ID miasta natomiast pozniej jest jakas lipa sad.gif
searchController ma 1 akcje:

Cytat
/**
* @Route("/search/city", name="search_city")
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function searchCityAction(Request $request)
{
$search = new City();

$form = $this->createForm(CityType::class, $search)
->add('submit', SubmitType::class, ["label" => "Szukaj"]);


$entityManager = $this->getDoctrine()->getManager();


if($request->isMethod(Request::METHOD_POST))
{
$form->handleRequest($request);

$selectedCity = ($_REQUEST['city']['city']);

$address = $entityManager->getRepository(Address::class)->findBy(["city" => $selectedCity]);
$searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $address]);

return $this->render("Search/searched.html.twig", ["searched" => $searched]);
}


return $this->render("Search/index.html.twig", ["form" => $form->createView()]);
}


Dostalem jedna rade zmienic controller tak:

Cytat
/**
* @Route("/search/city", name="search_city")
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function searchCityAction(Request $request)
{
$search = new City();

$form = $this->createForm(CityType::class, $search)
->add('submit', SubmitType::class, ["label" => "Szukaj"]);


$entityManager = $this->getDoctrine()->getManager();


if($request->isMethod(Request::METHOD_POST))
{
$form->handleRequest($request);

$selectedCity = ($_REQUEST['city']['city']);

$address = $entityManager->getRepository(Address::class)->findBy(["city" => $selectedCity]);
$addressIds = $address->map(function($single_address) {
return $single_address->id;
});
$searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $addressIds]);

return $this->render("Search/searched.html.twig", ["searched" => $searched]);
}


return $this->render("Search/index.html.twig", ["form" => $form->createView()]);
}


Tylko ze przy takim rozwiazaniu dostaje errora:

Cytat
Call to a member function map() on array


Wie ktos moze co moze byc nie tak?
Go to the top of the page
+Quote Post
Pyton_000
post 6.05.2018, 12:27:24
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


No i? Masz dokładny komunikat błędu. $address jest tablicą obiektów a nie obiektem typu Iterator.

Tak powinno wyglądać

  1. $addressIds = array_map(function($single_address) {
  2. return $single_address->id;
  3. }, $address);
Go to the top of the page
+Quote Post
paprikano
post 6.05.2018, 12:40:56
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 6.05.2018

Ostrzeżenie: (0%)
-----


Cytat(Pyton_000 @ 6.05.2018, 13:27:24 ) *
No i? Masz dokładny komunikat błędu. $address jest tablicą obiektów a nie obiektem typu Iterator.

Tak powinno wyglądać

  1. $addressIds = array_map(function($single_address) {
  2. return $single_address->id;
  3. }, $address);


Super działa dzieki, tylko trzeba bylo jeszcze ten return $single_address->id zmienic na $single_address->getId() bo krzyczal ze id jest prywatna

Ten post edytował paprikano 6.05.2018, 13:06:07
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.04.2024 - 06:11