【问题标题】:Retrieving doctrine data from sfDoctrinePager object !从 sfDoctrinePager 对象中检索教义数据!
【发布时间】:2010-12-15 19:56:49
【问题描述】:

我在从教义寻呼机对象中检索数据时遇到了一些问题! 让我在下面描述它:

这是我的寻呼机查询:

        $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-&gt;Groups-&gt;name$data-&gt;LicensedVendors-&gt;status 检索 GroupsLicensedVendors 数据!它也没有显示任何错误或任何值!看起来它输出一个空字符串。它不应该像 Profile data 一样获得价值吗?

但是,当我通过设置hydrate查询时:

$q-&gt;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** 的情况下获取这些数据?将那些 GroupsLicense 表数据作为对象检索时我做错了什么?

这是上述表的架构定义:

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:
    ClientUser:
      class: sfGuardUser
      local: client_id
      foreign: id
      foreignAlias: LicensedVendors
      onDelete: cascade
      foreignType: many
      owningSide: true
    VendorUser:
      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

【问题讨论】:

    标签: symfony1 doctrine


    【解决方案1】:

    您可以尝试使用 ArrayAccess。

    意义

     foreach ($pager->getResults() as $data) {
    
    
            echo $data['username'] ;  //outputs 'username' from sfGuardUser table
            echo '<br />' ;
            echo $data['Profile'][0]['org_name'] ; //outputs 'Organization name' from sfGuardUserProfile table 
    

    或者问题可能是您的代码中缺少 [0],意思是

     echo $data->Profile[0]->org_name ; //outputs 'Organization name' from sfGuardUserProfile table 
    

    【讨论】:

    • 您指出的非常正确 Waleed! echo $data->LicensedVendors[0]->status 现在给我结果!但它的表现仍然与预期不同!根据查询,它带来了 4 个表数据,其中 'status' 字段类似于 'approved'、'pending'、NULL、NULL ! echo $data['l_status'] 代码的行为符合预期,但 echo $data->LicensedVendors[0]->status 输出:'approved'、'pending' ,'待定','待定'!所以它为最后 2 个 NULL 值输出 'pending' !是不是很奇怪?
    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2017-06-16
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多