Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][SF2] CreateQuery oraz Twig, Problem z wyświetleniem danych w Twigu
damianooo
post 9.07.2013, 13:53:26
Post #1





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


Witam ,

Mam dwa pytania:
1) Czy tak rozbudowane zapytanie SQL w moim Modelu jest ok , czy powinienem zrobić krótszy zapis ?
2) Jak odwołać się do pobranych danych w Twig'u ?

Controller:

  1. public function testAction()
  2. {
  3. $em = $this->getDoctrine()->getEntityManager();
  4. $offersObject = $em->getRepository('MyBetBundle:Offer')->findOffersByMatchday(1);
  5. return $this->render('MyBetBundle:Recommend:test.html.twig', array('offers' => $offersObject));
  6. }


Model (Repository)

  1. public function findOffersByMatchday($matchday)
  2. {
  3. $query = $this->getEntityManager()
  4. ->createQuery('
  5. SELECT o.id, t1.name, t2.name, c.name, r.host, r.draw, r.guest, r.host_draw, r.guest_draw,r.host_guest
  6. FROM My\BetBundle\Entity\Offer o
  7. JOIN o.ratio r
  8. JOIN o.category c
  9. JOIN o.meet m
  10. JOIN m.matchday mt
  11. JOIN m.team_1 t1
  12. JOIN m.team_2 t2
  13. WHERE mt.id = :matchday
  14. ')
  15. ->setParameters(array('matchday' => $matchday));
  16. return $query->getResult();
  17. }
  18. }


View:
  1. <ul>
  2. {% for offer in offers %}
  3. <li>{{offer.id}}</li>
  4. <li>{{offer.meet.team_1.name}}</li>
  5. <li>{{offer.meet.team_2.name}}</li>
  6. <li>{{offer.category.name}}</li>
  7. <li>{{offer.ratio.host}}</li>
  8. <li>{{offer.ratio.draw}}</li>
  9. <li>{{offer.ratio.guest}}</li>
  10. <li>{{offer.ratio.host_draw}}</li>
  11. <li>{{offer.ratio.guest_draw}}</li>
  12. <li>{{offer.ratio.host_guest}}</li>
  13. {% endfor %}
  14. </ul>


Non stop otrzymuję błędy typu:

  1. Item "meet" for "Array" does not exist in MyBetBundle:Recommend:test.html.twig at line 4


Relacje między encjami są takie:
Offer (tabela pośrednicząca dla tabel Meet i Ratio, które są w relacji wiele do wielu)
Category (tabela w relacji wiele do 1 z Offer)
Go to the top of the page
+Quote Post
damianooo
post 9.07.2013, 15:31:13
Post #2





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


OK dzięki.
Jak zastosowałem to :

  1. getArrayResult();



to zrobiłem tak w TWIG:

  1. <ul>
  2. {% for offer in offers %}
  3. <li>{{offer['id']}}</li>
  4. <li>{{offer['name']}}</li>
  5. <li>{{offer['host']}}</li>
  6. <li>{{offer['draw']}}</li>
  7. <li>{{offer['guest']}}</li>
  8. <li>{{offer['host_draw']}}</li>
  9. <li>{{offer['guest_draw']}}</li>
  10. <li>{{offer['host_guest']}}</li>
  11. {% endfor %}
  12. </ul>



i działa jak należy

Ten post edytował damianooo 9.07.2013, 15:45:08
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: 14.07.2025 - 20:19