Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Symfony 2 - Problem z Doctrine
M4ver7071
post
Post #1





Grupa: Zarejestrowani
Postów: 63
Pomógł: 0
Dołączył: 26.06.2014

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


Witam, chcę zrobić pole do szukania przedmiotu w Symfony 2. Napisałem kod który powinien działać poprawnie, ale zwraca mi pustą tablicę. Próbowałem już kilka sposób, taki produkt jest w bazie a nie wiem dlaczego zwraca pustą tablicę.


  1.  
  2. public function showAction(Request $request)
  3. {
  4. //$word = $request->query->get('keywords')['search'];
  5. $word = 'toshiba';
  6.  
  7. $repository = $this->getDoctrine()->getRepository('CustomExampleBundle:Product');
  8.  
  9. $query = $repository->createQueryBuilder('p')
  10. ->where('p.title= :word')
  11. ->setParameter('word', '%'.$word.'%')
  12. ->getQuery();
  13. $result = $query->getArrayResult();
  14.  
  15. print_r($result); die;
  16. // return $this->render('CustomExampleBundle:Search:search.html.twig', array(
  17. // 'entity' => $result));
  18. }

Proszę o pomoc.

Ten post edytował M4ver7071 19.02.2015, 20:18:17
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
pyro
post
Post #2





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


  1. $qb->expr()->like('p.title', ':word')


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
M4ver7071
post
Post #3





Grupa: Zarejestrowani
Postów: 63
Pomógł: 0
Dołączył: 26.06.2014

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


Możesz mi pokazać przykładową implementację, tego co napisałeś?
Go to the top of the page
+Quote Post
pyro
post
Post #4





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Przykłady na pewno są chociażby w dokumentacji Doctrine. Wystarczy włożyć odrobinę własnego wysiłku.


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
M4ver7071
post
Post #5





Grupa: Zarejestrowani
Postów: 63
Pomógł: 0
Dołączył: 26.06.2014

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


Wiem, tylko problem polega na tym, że mój kod nie realizuje się poprawnie nie wiem w którym miejscu robie błąd więc chciałbym to zweryfikować na konkretnym przykładnie.
Go to the top of the page
+Quote Post
kazmij
post
Post #6





Grupa: Zarejestrowani
Postów: 17
Pomógł: 1
Dołączył: 12.02.2010

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


Kolego Twój kod:

  1. $query = $repository->createQueryBuilder('p')
  2. ->where('p.title= :word')
  3. ->setParameter('word', '%'.$word.'%')
  4. ->getQuery();
  5. $result = $query->getArrayResult();


ja bym przerobił tak:

  1. $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
  2. $query = $repository->createQueryBuilder('p')
  3. ->where($qb->expr()->eq('p.title', $qb->expr()->literal($word) ))
  4. ->getQuery();
  5. $result = $query->getArrayResult();


natomiast gdyby miałby to być like to:

  1. $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
  2. $query = $repository->createQueryBuilder('p')
  3. ->where($qb->expr()->like('p.title', $qb->expr()->literal('%' . $word . '%') ))
  4. ->getQuery();
  5. $result = $query->getArrayResult();


u mnie zawsze tak działa.

Spróbuj.
Go to the top of the page
+Quote Post
M4ver7071
post
Post #7





Grupa: Zarejestrowani
Postów: 63
Pomógł: 0
Dołączył: 26.06.2014

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


Dzięki za pomoc smile.gif

Ten post edytował M4ver7071 20.02.2015, 20:04:46
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 Aktualny czas: 21.08.2025 - 19:21