2012-10-08 50 views
1

兩個表中檢索數據我有一個小問題就在這裏:從教義

$advList = Doctrine::getTable('Advertiser') 
     ->createQuery('a') 
     ->leftJoin('a.PrincCity p') 
     ->leftJoin('a.TourCity t') 
     ->leftJoin('a.Category') 
     ->orderBy('a.new DESC') 
     ->where('a.is_activated = ?', true) 
     ->andWhereNotIn('a.category_id', array(4, 5)) 
     ->andWhere('((t.slug IS NULL AND p.slug = ?) OR t.slug = ?)', array($city_slug, $city_slug)) 
     ->addOrderBy('a.created_at DESC'); 

    $this->pager = new sfDoctrinePager('Advertiser', 50); 
    $this->pager->setQuery($advList); 
    $this->pager->setPage($request->getParameter('page', 1)); 
    $this->pager->init(); 

這段代碼獲得的所有廣告商從一個城市(由ID表廣告客戶代表) - 現在我有第二個表廣告,市至極包含id,AdvertiserId和CityID,因此我可以爲一個廣告客戶設置多個城市(例如,帶有AdvertiserID 99和不同CityID的5行)。

問題是我需要從一個城市獲得所有廣告客戶,但上面的代碼只能在數據庫中的一個表中工作,所以我不知道如何在第二個(與其他城市)進行搜索。

回答

1

我不知道,我能理解你的問題,但我認爲,如果你有納米廣告城市關係明確定義:

Advertiser: 
    ... 
    relations: 
    Cities: { class: City, refClass: AdvertiserCity, local: advertiser_id, foreign: city_id } 

AdvertiserCity: 
    columns: 
    advertiser_id: { type: integer,...} 
    city_id: { type: integer,....} 
    ... 
    relations: 
    City: { local: city_id, foreign: id, foreignAlias: AdvertiserCities } 
    Advertiser: { local: advertiser_id, foreign: id, foreignAlias: AdvertiserCities } 

然後,查詢可以去像:

$advList = Doctrine::getTable('Advertiser') 
     ->createQuery('a') 
     ->innerJoin('a.Cities c') 
     ... 
     ->addWhere('c.id = ?',$city_id) 
     ...;