2014-09-05 46 views
1
$data = $this 
     ->entityManager 
     ->getRepository('io\V1\Rest\Advertisers\AdvertisersEntity') 
     ->findBy(
      array('camapId' => '1465' , 'campName'=>'mytest')     
     ); 

這裏想補充的條件camapId='1465' OR campName like '%mytest%'
而不是camapId='1465' AND campName ='mytest'
那麼,有沒有辦法做到這一點?添加或又像條件findby()

回答

0
$campIdsToFilter = $campIdsToFilterArray =array(); 
    $query = $this->entityManager->createQuery("SELECT DISTINCT a.campId FROM io\V1\Rest\Advertisers\AdvertisersEntity a where a.campName like'%mytest%' OR a.campId=1465"); 

    $campIdsToFilter = $query->getResult(); 
    foreach($campIdsToFilter as $ke=> $value){ 
    $campIdsToFilterArray[] =$value; 

    } 


    $data = $this 
      ->entityManager 
      ->getRepository('io\V1\Rest\Advertisers\AdvertisersEntity') 
      ->findBy(
       array('campId' => $campIdsToFilterArray) , 
       $pageSize, // limit 
       $pageSize * ($page - 1) // offset    
      ); 
0

如果您使用Doctrine2 DQL或Doctrine2 QueryBuilder,您可以根據需要使存儲庫中的搜索條件複雜化。您可以在Doctrine文檔中找到關於此here (DQL)here (QueryBuilder)的信息。

網上有很多關於如何使用DQL和QueryBuilder實現你的結果的例子。

我認爲這將是這樣的:

$data = $this->createQueryBuilder('a') 
      ->where('a.camapId = 1465') 
      ->orWhere('a.campName = mytest') 
      ->getQuery() 
      ->getResult();