1

I got a little problem here:

$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();

This code gets all advertisers from a city (represented by ID in table Advertiser) - now I have second table Advertiser-City wich contains id, AdvertiserId and CityID so I can have multiple cities for one advertiser (eg 5 rows with AdvertiserID 99 and different CityID).

The problem is that I need to get all advertisers from a city, but upper code can only work with one table in the DB so I don't know how to search in the second one (with the additional cities).

4

1 回答 1

1

我不确定我能理解你的问题,但我认为如果你有明确定义的 nm Advertiser-city 关系:

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)
        ...;
于 2012-11-19T15:06:24.030 回答