CakePHP Containable multidimensional array hierarchy

≯℡__Kan透↙ 提交于 2019-12-25 08:13:52

问题


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:


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.



来源:https://stackoverflow.com/questions/24604104/cakephp-containable-multidimensional-array-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!