如何使用从多个表中选择不同列的doctrine或symfony pager?

时间:2010-12-12 20:28:36

标签: symfony1 doctrine doctrine-1.2

当我的查询从两个或多个表中选择多个列时,如何使用Doctrine_Pager或sfDoctrinePager实现分页?


编辑1:

<击>

<击>

好的,现在我发现Nathan在下面的描述可以做到这一点!我感到困惑,因为我无法从查询中检索某些数据!让我在下面描述一下:

这是我的寻呼机查询:

        $pager = new sfDoctrinePager('sfGuardUser', '5'); 

        $q = Doctrine_Query::create()
                        ->select('u.id, u.username,  p.org_name,  g.name, l.status')
                        ->from('sfGuardUser u')
                        ->leftJoin('u.Profile p')
                        ->leftJoin('u.Groups g')
                        ->leftJoin('u.LicensedVendors l')
                        ->where('g.name = \'client\'');

        $pager->setQuery($q);
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();

现在在我的模板中,我可以检索我的sfGuardUser和Profile数据,如下所示:

 foreach ($pager->getResults() as $data) {


            echo $data->username ;  //outputs 'username' from sfGuardUser table
            echo '<br />' ;
            echo $data->Profile->org_name ; //outputs 'Organization name' from sfGuardUserProfile table 

} 

我错误地尝试按$data->org_name检索个人资料数据,而不是$data->Profile->org_name!现在它正确地为这部分工作,但仍有问题

我仍无法检索群组&amp;使用$data->Groups->name$data->LicensedVendors->status LicensedVendors 数据!它也没有显示任何错误或任何值!看起来它输出一个空字符串。它不应该像Profile数据一样获得价值吗? 但是,当hydrate通过设置$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);查询时,

foreach ($pager->getResults() as $data) { echo $data['u_username']; echo $data['p_org_name']; echo $data['g_name']; echo $data['l_status']; }

我可以通过以下方式检索所有数据:

**Doctrine_Core::HYDRATE_SCALAR**

如何在不设置License: actAs: [Timestampable] tableName: licenses columns: id: type: integer(4) primary: true notnull: true autoincrement: true status: type: enum values: ['approved','pending_admin','pending_client','pending_vendor','rejected'] default: 'pending' client_id: type: integer(8) notnull: true vendor_id: type: integer(8) notnull: true product_desc: type: clob(16777215) supplier_name: type: string(80) other_desc: type: string(50) financial_statement: type: clob relations: VendorUser: class: sfGuardUser local: client_id foreign: id foreignAlias: LicensedVendors onDelete: cascade foreignType: many owningSide: true ClientUser: class: sfGuardUser local: vendor_id foreign: id foreignAlias: LicensedClients onDelete: cascade foreignType: many owningSide: true sfGuardUser: actAs: [Timestampable] columns: first_name: string(255) last_name: string(255) email_address: type: string(255) notnull: true unique: true username: type: string(128) notnull: true unique: true algorithm: type: string(128) default: sha1 notnull: true salt: string(128) password: string(128) is_active: type: boolean default: 1 is_super_admin: type: boolean default: false last_login: type: timestamp indexes: is_active_idx: fields: [is_active] relations: Groups: class: sfGuardGroup local: user_id foreign: group_id refClass: sfGuardUserGroup foreignAlias: Users sfGuardUserProfile: actAs: Timestampable: ~ columns: user_id: type: integer notnull: true email: type: string(80) notnull: true unique: true email_new: type: string(80) unique: true firstname: type: string(30) lastname: type: string(70) org_name: type: string(80) notnull: true relations: User: class: sfGuardUser foreign: id local: user_id type: one onDelete: cascade foreignType: one foreignAlias: Profile sfGuardGroup: actAs: [Timestampable] columns: name: type: string(255) unique: true description: string(1000) relations: Users: class: sfGuardUser refClass: sfGuardUserGroup local: group_id foreign: user_id foreignAlias: Groups 的情况下获取这些数据?我在检索那些群组 LicensedVendors 表格数据时出错?

以下是上述表格的架构定义:

{{1}}

<击>


编辑2:我将我在第一次编辑中描述的新问题发布为单独的问题here

2 个答案:

答案 0 :(得分:1)

我想只要您的查询返回一个Doctrine_Collection对象,就可以将它与寻呼机一起使用,不是吗?

答案 1 :(得分:0)

是的,greg0ire说的是什么。 This documentation is a bit old,但它显示了过去Propel需要的东西。更新到Doctrine就像,

public function executeList ()
{
  $pager = new sfDoctrinePager('Comment', 2);

  $q = Doctrine_Core::getTable('Comment')
    ->createQuery('c')
    ->where('c.author = ?', 'Steve')
    ->leftJoin('c.Article a')
    ->andWhere('a.content LIKE ?', '%enjoy%')
    ->orderBy('c.created_at ASC');

  $pager->setQuery($q);
  $pager->setPage($request->getParameter('page', 1));
  $pager->init();

  $this->pager = $pager;
}

This blog post, "Symfony doctrine pager for two tables"有一个更加扩展/复杂的例子。哦,看起来这是作者对his own SO question的回答。