0

我需要从模型中获取多条记录,然后将它们放入 request->data 以便视图呈现为具有多个输入字段集的表单,例如 name='data[Applicant][0][display_name]'。name='data[Applicant][1][display_name]'...为每个申请人输入数据值。

其实我已经做了我想做的事,但我认为这不是一个好方法。感谢是否有人可以指导我

        foreach ($this->Applicant->data['Applicant'] as $key=>$item){
            $data['Applicant'][] = $item['Applicant'];
        }
        $this->request->data = $data;//set Model to data
        $this->set('data' , $this->Applicant->data);

$this->Applicant->data 如下:

    Array
(
    [Applicant] => Array
        (
            [0] => Array
                (
                    [Applicant] => Array
                        (
                            [id] => 1
                            [application_id] => 17
                            [name] => User
                            [first_name] =>
...
                    )

            )

        [1] => Array
            (
                [Applicant] => Array
                    (
                        [id] => 3
                        [application_id] => 17
                        [name] => 
                        [first_name] => 

以下是所需的输出(少一级):

Array
(
    [Applicant] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [application_id] => 17
                    [name] => User
                    [first_name] => 
...

                )

            [1] => Array
                (
                    [id] => 3
                    [application_id] => 17
                    [name] => 
                    [first_name] =>

谢谢

4

1 回答 1

1

这应该足够了:

$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );
于 2013-08-29T18:17:40.880 回答