简体   繁体   English

从Doctrine中的两个表中检索数据

[英]Retrieving data from two tables in Doctrine

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). 此代码从一个城市获取所有广告客户(在表Advertiser中由ID表示)-现在我有了第二个表Advertiser-City,其中包含id,AdvertiserId和CityID,因此我可以为一个广告商拥有多个城市(例如5个广告客户ID为99的行, 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). 问题是我需要从一个城市获取所有广告商,但是上层代码只能与数据库中的一个表一起使用,因此我不知道如何在第二个表中(与其他城市一起)进行搜索。

I'm not sure that I can understand your problem, but I think that if you have the nm Advertiser-city relation well defined: 我不确定我是否能理解您的问题,但是我认为,如果您已经明确定义了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 } 

Then, your query can go like: 然后,您的查询可以像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM