如何使用从多个表中选择不同列的原则或symfony寻呼机?

发布于 2024-10-07 13:44:00 字数 5077 浏览 3 评论 0原文

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


Edit1:

好的,现在我明白了这可以按照内森下面描述的方式完成!我很困惑,因为我无法从查询中检索某些数据!让我在下面描述一下:

这是我的寻呼机查询:

        $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 和配置文件数据:

 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!现在这部分工作正常,但仍然存在问题

我仍然无法检索群组 &使用 $data->Groups->name$data->LicensedVendors->statusLicensedVendors 数据!它也没有显示任何错误或任何值!看起来它输出一个空字符串。它不应该像 Profile data 一样获取值吗? 但是,当我通过设置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** ?我在检索这些GroupsLicensedVendors表数据时做错了什么?

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

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


编辑2:我发布了我的新问题,我在第一次编辑中将其描述为一个单独的问题这里

How can I implement pagination using Doctrine_Pager or sfDoctrinePager while my query selects multiple columns from two or more tables ?


Edit1:

Ok, now I figured out that it can be done how Nathan has described below! I got confused as I couldn't retrieve certain data from the query! Let me describe it below:

This is my pager query:

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

Now in my Template I can retrieve my sfGuardUser and Profile data like this:

 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 

} 

I was wrongly trying to retrieve the profile data by $data->org_name and not $data->Profile->org_name! Now its working for this part correctly, but there is still an issue !

I am still unable to retrieve the Groups & LicensedVendors data using $data->Groups->name or $data->LicensedVendors->status ! It does not show any error or any value either! looks like it outputs an empty string. Shouldn't it get the value just like Profile data ?
But when I hydrate the query by setting:

$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);

I can retrieve all data through:

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

      echo $data['u_username'];
      echo $data['p_org_name'];
      echo $data['g_name'];
      echo $data['l_status'];
}

How to get those data without setting **Doctrine_Core::HYDRATE_SCALAR** ? Where I'm doing wrong for retrieving those Groups and LicensedVendors table data?

Here is the schema definition of the tables described above:

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


Edit2: I posted my new issues which I described in first edit as a separate question here !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-10-14 13:44:00

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

I guess as long as your query gives back a Doctrine_Collection object, you can use it with a pager, can't you?

雨落星ぅ辰 2024-10-14 13:44:00

是的,greg0ire 说的。 此文档有点旧,但它显示了您的内容过去需要 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;
}

这篇博文“两个表的 Symfony 学说寻呼机” 有一个更扩展/复杂的示例。哦,看起来这是作者对他自己的问题的回答。

Yeah, what greg0ire said. This documentation is a bit old, but it shows what you'd need with Propel in the old days. Updating to Doctrine would be like,

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" has a more extended/convoluted example. Oh, looks like that was the author's answer to his own SO question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文