2016-04-15 62 views
0

上調用方法「createQueryBuilder」我有一個實體「Vehicules」,它與實體「User」具有ManyToOne關係。所以每個用戶都有一個或多個車輛。我試圖計算每個用戶的車輛數量並試圖將其顯示在表格中。 這是我的實體的一部分VEHICULE試圖在類

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

這是我想算VEHICULES的foreach用戶(方向)的數量,並顯示在一個表

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)); 
    } 

我得到的功能這個錯誤

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」它的工作原理,但我想爲它獲得所有我的車輛我怎麼能做到這一點? –