2012-09-18 47 views
0

,所以我有以下蒙戈ODM查詢工作得很好:無法向上突破主義ODM查詢

 $query = $dm->createQueryBuilder('MainClassifiedBundle:Listing') 
     ->select('id', 'title', 'assets') 
     ->field('somefield0')->equals($somefield) 
     ->field('somefield')->equals($blah) 
     ->field('somefield2')->range($minPrice, $maxPrice) 
     ->field('somefield3')->near($latitude, $longitude) 
     ->getQuery(); 

$my_data = $query->execute(); 

但是,如果我試圖打破它像下面這樣:

$query = $dm->createQueryBuilder('MainClassifiedBundle:Listing') 
    ->select('id', 'title', 'assets') 
    ->field('somefield0')->equals($somefield) 
    ->field('somefield')->equals($blah); 

if ($propertyType != 'All') { 
    $query->field('someothercrazyfield')->equals($somethingelse); 

    $query->field('somefield2')->range($minPrice, $maxPrice) 
    ->field('somefield3')->near($latitude, $longitude) 
    ->getQuery(); 

$my_data = $query->execute(); 

我得到一個方法執行不存在的錯誤。

爲什麼?

回答

2

因爲在調用getQuery()時不存儲查詢。我已經改變了你的例子。

$query = $dm->createQueryBuilder('MainClassifiedBundle:Listing') 
    ->select('id', 'title', 'assets') 
    ->field('somefield0')->equals($somefield) 
    ->field('somefield')->equals($blah); 

if ($propertyType != 'All') { 
    $query->field('someothercrazyfield')->equals($somethingelse); 

    $query->field('somefield2')->range($minPrice, $maxPrice) 
     ->field('somefield3')->near($latitude, $longitude); 
} 

$my_data = $query->getQuery()->execute();