CakePHP Containable multidimensional array hierarc

2019-09-04 02:23发布

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条回答
虎瘦雄心在
2楼-- · 2019-09-04 02:42

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.

查看更多
登录 后发表回答