2

我在处理 Doctrine 中的视图时遇到了问题。

我的数据库中有以下视图:

$q = Doctrine_Query::create()
                        ->select('c.id, c.name, m.id, m.name, m.status, 
                                  m.modified, m.entered, m.accountid')
                        ->from('Category c')
                        ->leftJoin('c.Mailing m');
$view = new Doctrine_View($q, 'view_mailings');
$q->execute(array(), Doctrine::HYDRATE_ARRAY);

结果包括所有记录。但是,我需要根据 accountid 的不同值获取一个子集,并按不同的列排序。我怎样才能做到这一点?

谢谢你的帮助。

4

2 回答 2

2

一个不同的解决方案可能是为 config/doctrine/ 中的视图创建一个模式,如下所示:

objective:
  tableName: objective
  columns:
    id:
      type: integer
    zone:
      type: string(50)
    province:
      type: string(30)
    city:
      type: string(100)
    population:
      type: integer
    city_obj_id:
      type: integer
    clinic_number:
      type: integer
    clinic_obj:
      type: integer
    comments:
      type: string(255)

然后在 data/sql/ 中放置一个objective_view.sql,如下所示:

DROP VIEW IF EXISTS objective;
DROP TABLE IF EXISTS objective;
CREATE VIEW objective AS
    SELECT
    c.id as id,
    z.name as zone,
    p.name as province,
    c.name as city,
    cs.amount as population,
    co.id as city_obj_id,
    co.clinic_num as clinic_number,
    co.clinic_obj as clinic_obj,
    co.comments as comments

    FROM
    city c left join city_objective co on co.city_id = c.id
    left join city_census cs on (cs.city_id = c.id and cs.year = 2009)
    left join province p on p.id = c.province_id
    left join zone z on z.id = p.zone_id;

通过这种方式,目标表将填充更新的数据,您可以对其执行查询以轻松获取相关信息。

于 2010-11-23T14:45:14.083 回答
1

您只是在执行查询 $q,而不使用您创建的视图。

于 2010-02-26T20:16:26.743 回答