dongpao9437 2014-01-30 10:08
浏览 33
已采纳

查询查询中返回奇怪的数据结构

I have the following Models with the following relations between them (I just posted the relevant information about them).

Persona.php

public $hasMany = array(
   'PersonaHasLdaphost' => array(
      'className' => 'PersonaHasLdaphost',
      'foreignKey' => 'persona_id'
   )
);

Ldaphost.php

public $hasMany = array(
   'PersonaHasLdaphost' => array(
      'className' => 'PersonaHasLdaphost',
      'foreignKey' => '__ldaphosts_id',
      'dependent' => false
   )
);

PersonaHasLdaphost.php

public $belongsTo = array(
   'Persona' => array(
      'className' => 'Persona',
      'foreignKey' => 'persona_id',
   ),
   'Ldaphost' => array(
      'className' => 'Ldaphost',
      'foreignKey' => '__ldaphosts_id',
   )
);

I have other models, even Persona itself, with this kind of relation working just fine.

But with those ones, when I query the database with a find:

$this->Persona->Behaviors->load('Containable');
$options = array('conditions' => array('Persona.' . $this->Persona->primaryKey => $id),
   'contain' => array(
      'Personaacceso',
      'Personainterna',
      'PersonaHasLdaphost' => array('Ldaphost')),
   'recursive'=>1);
$persona = $this->Persona->find('first', $options);

I get this weird ouput:

["PersonaHasLdaphost"]=> array(2) {
   [0]=> array(4) {
      ["id"]=> string(3) "154"
      ["persona_id"]=> string(3) "315"
      ["Ldaphost"]=> array(0) {}
      ["PersonaHasLdaphost"]=> array(1) {
         [0]=> array(1) {
            ["__ldaphosts_id"]=> string(2) "41"
         }
      }
   }
   [1]=> array(4) {
      ["id"]=> string(3) "174"
      ["persona_id"]=> string(3) "315"
      ["Ldaphost"]=> array(0) {}
      ["PersonaHasLdaphost"]=> array(1) {
         [0]=> array(1) {
            ["__ldaphosts_id"]=> string(3) "120"
         }
      }
   }
}

When it should be something like:

["PersonaHasLdaphost"]=> array(2) {
   [0]=> array(4) {
      ["id"]=> string(3) "154"
      ["persona_id"]=> string(3) "315"
      ["__ldaphosts_id"]=> string(2) "41"
      ["Ldaphost"]=> array(0) {} 
   }
   ...

With data inside "Ldaphost" of course, cause there is an entry in the ldaphost table for those ids.

So can anyone give me a hint why this is happening? I can't see why this one is throwing different results than the others.

  • 写回答

1条回答 默认 最新

  • duanlu9557 2014-02-03 09:10
    关注

    I finally managed to solve it.

    Seems cakePHP doesn't like dealing with database tables and/or column names starting with double underscore: __ldaphosts table and __ldaphosts_id.

    Once I tried with both names modified, my data was returned correctly:

    ["PersonaHasLdaphost"]=> array(1) {
       [0]=> array(4) {
          ["id"]=> string(3) "165"
          ["persona_id"]=> string(3) "455"
          ["ldaphosts_id"]=> string(3) "120"
          ["Ldaphost"]=> array(8) {
             ["id"]=> string(3) "120"
             ["hostname"]=> string(7) "xxxx"
             ["ip"]=> string(14) "xxx.xx.xxx.xxx"
             ["mac"]=> string(17) "xx:xx:xx:xx:xx:xx"
             ["tipo"]=> string(6) "server"
             ["encendible"]=> bool(false)
             ["apagable"]=> bool(false)
             ["descripcion"]=> string(24) "xxxxxxxxxxxxxxxxxxxxxxx"
          }
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作