0

I can't get the values from a second level contain using different field name as link.

Asistencia model:

var $belongsTo = array('Employee');

Horario model:

var $belongsTo = array('Employee');

Employee model:

        var $hasMany = array(
            'Horario' => array(
                'foreignKey' => 'employee_id',
                'dependent' => true
            ),
            'Asistencia' => array(
                'foreignKey' => 'employee_id',
                'dependent' => true
            )
        );

I'll explain using these values on my example record:

Asistencia: employee_id = 3701
Employee  : id          = 3701

In my find() from Asistencia, I get to contain Employee by switching Employee primaryKey just fine:

    $this->Asistencia->Employee->primaryKey = 'id';

    $this->paginate = array(
        'contain' => array(
            'Employee' => array(
                'foreignKey' => 'employee_id',
                //'fields' => array('id', h('emp_ape_pat'), h('emp_ape_mat'), h('name')),
             'Horario' => array(
                    'foreignKey' => 'employee_id',
                //'fields' => array('id' )
            ))
        ),
        'conditions' => $conditions,
        'order' => 'Asistencia.employee_id');

However, my Horario record is linked to Employees via another field: emp_appserial

Employee  : emp_appserial         =  373
Horario   : employee_id           =  373

How can my Employee array contain Horario array? (they do share the value just mentioned).

Currently, the Horario contain is using the value on Asistencia.employee_id and Employee.id (3701). (checked the sql_dump and is trying to get the Horario via

"WHERE `Horario`.`employee_id` = (3701)" 

but for the Employee to contain Horario, it should use the value on Employee.emp_appserial and Horario.employee_id (373).

This is what i get (empty array at bottom)

array(
(int) 0 => array(
    'Asistencia' => array(
        'id' => '5',
        'name' => null,
        'employee_id' => '3701',
        'as_date' => '2012-11-19',
    ),
    'Employee' => array(
        'id' => '3701',
        'emp_appserial' => '373',
        'emp_appstatus' => '8',
        'AgentFullName' => '3701 PEREZ LOMELI JORGE LORENZO',
        'FullNameNoId' => 'PEREZ LOMELI JORGE LORENZO',
        'Horario' => array()
    )))

Please notice:

'employee_id' => '3701',  (Asistencia)

and

'emp_appserial' => '373',  (Employee)

my Horario has 'employee_id' = 373.

How could I make the switch so the relation Employee<->Horario is based on emp_appserial? Thank you in advance for any help.

Carlos Garcia
  • 359
  • 1
  • 5
  • 29
  • As a workaround, I'll have Horario.employee id to match employee.id and use new field: Horario.emp_appserial for this other identifier (value 373 in example). However this change on field use calls for a through refactoring - i just hope is not too-much :-) This will straight things up though. – Carlos Garcia Nov 21 '12 at 18:38

1 Answers1

0

Firstly you may be getting problems because you're using Spanish words for your Model names and I suspect you're also using them for Controllers.

This is a very bad practice since CakePHP's idea is:

Convention over configuration.

This is achieved through the Inflector class which "takes a string and can manipulate it to handle word variations such as pluralizations or camelizing and is normally accessed statically". But this works ONLY WITH English words. What this means for you is that you may be missing the data because Cake is not able to build the DB queries right, since you're using Spanish words. By doing so you're making the use of CakePHP's flexible persistence layer obsolete - you will have to make all the configurations by hand. Also most likely pagination will NOT WORK. Other parts of the framework may also not work properly: HtmlHelper, FormHelper, Components, etc. ...

These problems will expand if you try to use more complex Model associations such as HABTM or "hasMany through the Join Model". So I do not know why exactly you aren't seeing the Horario record, but what I just explained most likely is your problem.

What you're trying to do is against the core principles of CakePHP and you'd save yourself a lot of problems if you refactor a bit and use English words. Your other option is NOT TO use Cake.

Borislav Sabev
  • 4,776
  • 1
  • 24
  • 30
  • thank you. For spanish name models, as long as i've stick to singular, plural, camelcase,etc, cakephp has has behaved just fine and provided all the magic.-I see no problem there-. AS for my find issue,switching employee model primary key has worked great for me; it's just that in this instance, my find's contain needed to use the "regular" primary key (as opposed to the switch) in order to get my horario. What I did was refactor to have horario table include both employee.id and employee.app_serial so the contain (employee-horario) is based on the same pk as asistencia->employee. – Carlos Garcia Nov 25 '12 at 05:43