0
votes

If I do this:

$cdata = $this->Party->find('first',
        array('contain' => array(
            'Person' => array(
                'Employee' => array(
                    'Volunteer'),
                'Title'))));
Debugger::dump($cdata, 10);

I get this:

array(
    'Party' => array(
        'id' => '9',
        'save_bit' => true
    ),
    'Person' => array(
        'id' => '5',
        'title_id' => '1',
        'first_name' => 'bob',
        'middle_name' => '',
        'last_name' => 'brown',
        'is_active' => false,
        'date_of_birth' => '1999-07-07',
        'gender' => 'M',
        'party_id' => '9',
        'Title' => array(
            'id' => '1',
            'title' => 'Ms',
            'description' => ''
        ),
        'Employee' => array(
            'id' => '5',
            'person_id' => '5',
            'Volunteer' => array(
                'id' => '5',
                'employee_id' => '5'
            )
        )
    )
)

But I really want to get this:

array(
    'Party' => array(
        'id' => '9',
        'save_bit' => true,
        'Person' => array(
            'id' => '5',
            'title_id' => '1',
            'first_name' => 'bob',
            'middle_name' => '',
            'last_name' => 'brown',
            'is_active' => false,
            'date_of_birth' => '1999-07-07',
            'gender' => 'M',
            'party_id' => '9',
            'Title' => array(
                'id' => '1',
                'title' => 'Ms',
                'description' => ''
            ),
            'Employee' => array(
                'id' => '5',
                'person_id' => '5',
                'Volunteer' => array(
                    'id' => '5',
                    'employee_id' => '5'
                )
            )
        )
    )
)

This way the array is following the natural hierarchy of my database tables and also works with my website forms for saving and editing which I have declared like so:

echo $this->Form->input('Party.Person.first_name');
echo $this->Form->input('Party.Person.is_active');

Is it possible to do this with Containable, if so how? Thanks!

1
No, it's not possible with containable. But why do you want to work against the CakePHP conventions in the first place, it will only cause trouble!? ps. please always mention your exact CakePHP version and tag your question accordingly. - ndm
Thanks I've added the version tag. I wanted the array like this because, due to the way my form controls are named, that is how it looks like when the form submits to the controller. So for an edit view, if I send an array like this to the form it works fine, however I believe if Person is not encapsulated in the Party hierarchy then it gives me problems and doesn't populate the fields properly. I didn't realise I was going against conventions here. Do you have a suggestion on how I should tackle this then? Perhaps change the way I have named the controls so it's just Person.first_name etc? - nuutrino

1 Answers

0
votes

As already mentioned in my comment, you cannot change the way how containable formats the data, it will be formatted to the CakePHP conventions depending on your associations.

Changing the format would have to be done manually after the find operation, but there's actually no need to do that, instead stick to the conventions with regards to associations and form helper usage, that way CakePHP can automatically glue everything together as needed.

Assuming the association is Party hasMany Person, you should simply use Person.0.first_name in the form, that way the helper can properly populate the field, and when passed to Model::saveAll() or Model::saveAssociated() CakePHP can determine how things need to be saved based on the associations defined on your models.

See Saving Related Model Data (hasOne, hasMany, belongsTo) for more information.