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:
public function testAction()
{
$em = $this->getDoctrine()->getEntityManager();
$offersObject = $em->getRepository('MyBetBundle:Offer')->findOffersByMatchday(1);
return $this->render('MyBetBundle:Recommend:test.html.twig', array('offers' => $offersObject)); }
Model (Repository)
public function findOffersByMatchday($matchday)
{
$query = $this->getEntityManager()
->createQuery('
SELECT o.id, t1.name, t2.name, c.name, r.host, r.draw, r.guest, r.host_draw, r.guest_draw,r.host_guest
FROM My\BetBundle\Entity\Offer o
JOIN o.ratio r
JOIN o.category c
JOIN o.meet m
JOIN m.matchday mt
JOIN m.team_1 t1
JOIN m.team_2 t2
WHERE mt.id = :matchday
')
->setParameters(array('matchday' => $matchday)); return $query->getResult();
}
}
View:
<ul>
{% for offer in offers %}
<li>{{offer.id}}</li>
<li>{{offer.meet.team_1.name}}</li>
<li>{{offer.meet.team_2.name}}</li>
<li>{{offer.category.name}}</li>
<li>{{offer.ratio.host}}</li>
<li>{{offer.ratio.draw}}</li>
<li>{{offer.ratio.guest}}</li>
<li>{{offer.ratio.host_draw}}</li>
<li>{{offer.ratio.guest_draw}}</li>
<li>{{offer.ratio.host_guest}}</li>
{% endfor %}
</ul>
Non stop otrzymuję błędy typu:
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)