2017-05-03 31 views
0

我有一個關係多對多表progvol,表用戶,表航班 和一對多表用戶關係rammasage 在表rammassage我有一個日期字段,並在表航班我有一個開始日期我已經比較這兩個日期 因此,我寫了一個SQL查詢來知道VOL表的用戶的日期。 首先j我的控制器獲取當前用戶$ user = $ this-> getUser(); 在我repositroy我在控制我做Symfony2知識庫查詢

$comp = new CompanyEvents(); 

$user = $this->get('security.token_storage')->getToken()->getUser(); 
$userprogvol = $em->getRepository('PfeBundle:programmevol')->getOnlyActive($user->getId(),$comp->getId()); 

現在我有我有programvol表的飛行ID +當前的ID的$ userprogvol表做

<?php 

namespace PfeBundle\Repository; 
use Doctrine\ORM\EntityRepository; 


/** 
* programmevolRepository 
* 
* This class was generated by the Doctrine ORM. Add your own custom 
* repository methods below. 
*/ 
class programmevolRepository extends EntityRepository 
{ 

    public function getOnlyActive ($valeur,$vol) 
    { 
     $qb = $this->createQueryBuilder('a'); 
     $qb->where('a.users = :valeur'); // means id_user of table programmevol = $valeur($user->getId()); 
     $qb->where('a.vols = :vol'); // means id_vol of table vol = $vol (une instancedelaclassevol->getId()); 
     $qb->where('vol' , $vol); 
     $qb->setParameter('valeur', $valeur); 

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


} 

用戶

現在我想要做的(如果$ userprog-> flights(id_vol)== $ comp-> id_vol)$ var = $ comp-> getDebt();但$ userprogvol是一個數組?

我該如何做到這一點?還有另一個竅門(查詢的返回類型是對象)?

+0

請花時間來解決你的問題的格式化,這是難以閱讀。 – Growiel

回答

0

好,如果它是一個數組,你可以只重複它

foreach ($userprog->flights() as $flight) { 
    if($flight->getId() == $comp->getId()){ 
     //... 
    } 
} 

,因爲你的變量和財產的命名是完全以我不清楚,這只是爲了讓你得到一個想法

,它是不是簡單的數組,但一個學說的ArrayCollection 所以在這裏你有可用的方法列表

http://www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.ArrayCollection.html

也許$userprog->flights()->contains($element)

是什麼youre真正追求