本文介绍了CakePHP可容纳多维数组层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做:

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

我得到:

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'
            )
        )
    )
)

但我真的想得到这个:

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'
                )
            )
        )
    )
)

$ b b

这种方式数组遵循我的数据库表的自然层次结构,也与我的网站表单进行保存和编辑,我已经这样声明:

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');

是否可以用Containable做到这一点,谢谢!

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

推荐答案

如我在注释中提到的,你不能改变数据格式的格式, CakePHP约定取决于您的关联。

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.

假设关联是 Party hasMany Person

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.

请参阅。

这篇关于CakePHP可容纳多维数组层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:42