wildnano
wildnano

Reputation: 21

Symfony createQueryBuilder JOIN

I'm trying to get my products from repository I have 3 tables: - categories - types which contains categorie_id - produits which contains types_id

My twig view returns "variable produits does not exist" What's going wrong in my querybuilder ? Thanks

class ProduitsRepository extends \Doctrine\ORM\EntityRepository {

    public function byCategorie($categorie) {
        $qb = $this->createQueryBuilder('p')
                ->select('p') // Entity Produits
                ->join('p.type', 't')
                ->addSelect('t') // Entity Types
                ->where('p.type = t.id AND t.categorie = :categorie')
                ->orderBy('p.id')
                ->setParameter('categorie', $categorie);

        return $qb->getQuery()->getResult();      
    }
} 

ok i add a screenshot enter image description here

This is my controller

public function categorieAction($categorie)
{
    $em = $this->getDoctrine()->getManager();
    $produits = $em->getRepository('GbaBundle:Produits')->byCategorie($categorie);


    return $this->render('GbaBundle:Default:produits/layout/produits.html.twig', array(
        'produits' => $produits
    ));
}

This is my twig view produits.html.twig

<ul class="thumbnails">
             {% for produit in produits %}
            <li class="span3">
                <div class="thumbnail">
                    <img src="{{ asset('img/holder.png') }}" alt="" width="300" height="300">
                    <div class="caption">
                        <h4>Thumbnail label</h4>
                        <p>100,00 €</p>

                        <a class="btn btn-primary" href="{{ path('gba_presentation') }}">Plus d'infos</a>

                        <a class="btn btn-success" href="{{ path('gba_panier') }}">Ajouter au panier</a>
                    </div>
                </div>
            </li>
              {% endfor %}
            </ul>

Upvotes: 0

Views: 4697

Answers (1)

A. Atallah
A. Atallah

Reputation: 35

Sorry I can't post comments but you should give us your twig file, it might help us. Also, I don't think it will change anything but I would write the query like this:

$this->createQueryBuilder('p')
        ->select('p, t')
        ->join('p.type', 't')
        ->where('t.categorie = :categorie')
        ->orderBy('p.id')
        ->setParameter('categorie', $categorie);

Edit: Looking at your screenshot, it seems like you are rendering your view from the wrong controller action. You have one action called categorieAction and another one (which seems to be the wrong one) called produitsAction. You should make different routes for these two actions to avoid collisions.

Upvotes: 1

Related Questions