Weird data structure returned in find query

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.

Answers


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"
      }
   }
}

Need Your Help

Django Models not Showing Up in DB after syncdb

django model syncdb

I have a models folder that has a few models in files that are already in the DB. I have just added another file/model but it is not being added to the DB when I run syncdb. I've tried manage.py va...

Return the count of a collection

python mongodb

How can I turn db.collection.count() into a Python script?