2016-04-15 7 views
0
に呼び出そうとしました

エンティティ "Vehicules"は、エンティティ "User"とManyToOne関係にあります。したがって、各ユーザーは1つ以上の車両を所有しています。私はforeachユーザーの台数を数え、それをテーブルに表示しようとしています。 これはこれは私がvehicules foreachのユーザー(方向)の数をカウントし、私が得るクラス "createQueryBuilder"をクラス

public function afficheAction() { 
     $em = $this->getDoctrine(); 
     $demandes = $em->getRepository('DemandevehBundle:Demande')->findAll(); 
     // trying to count the number of vehicules foreach direction  
     $vehicules = $this->getDoctrine()->getRepository('CarPfeBundle:Vehicule')->findAll(); 
     foreach($vehicules as $vehicule) { 
     $qb = $vehicule->createQueryBuilder('v'); 
     $qb->select('count(v.id)'); 
     $qb->where('v.direction = ?1'); 
     $qb->setParameter('1', $vehicule->getId()); 
     $query = $qb->getQuery(); 
     $result = $query->getSingleScalarResult(); 
     return $result; 
     } 
     // fin 
     return $this->render('DemandevehBundle:Demande:affiche.html.twig', array('demandes' => $demandes 
        ,'result' => $result)); 
    } 

テーブルにそれを表示したい機能がある

/** 
    * @ORM\ManyToOne(targetEntity="OC\UserBundle\Entity\User") 
    * @ORM\JoinColumn(name="User_id", referencedColumnName="id", onDelete="CASCADE") 
    */ 
    protected $direction; 

VEHICULE

私のエンティティの一部でありますこのエラー

Attempted to call method "createQueryBuilder" on class "Car\PfeBundle\Entity\Vehicule". 

私はこのエラーが発生する理由が私の機能は意味をなしません。助けてください?

答えて

0

エンティティではなくエンティティマネージャでクエリビルダを作成することは可能です。

$qb = $this->getDoctrine() 
    ->getRepository('CarPfeBundle:Vehicule') 
    ->createQueryBuilder('v'); 

の代わりに、この:だから、そう、これを試すクラスの相対的なマネージャーを使用

$qb = $vehicule->createQueryBuilder('v'); 

・ホープ、このヘルプ

+0

実際に私は、それがどのように役立つ可能性が混乱しています。より多くのspeceficするために私は "$ vehicule-> getId()"を "5"に変更したので、ただ1つの変数にしようとしましたが動作しますが、私はどのようにそれを行うことができますか? –

関連する問題