0

I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.

Actually I've already done what i want, but i do not think it is a good method to do so. Appreciate if anyone can guide me

        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 is the following:

    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] => 

the following is the desired output (less one level):

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

                )

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

thanks

1 Answer 1

1

This should suffice:

$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.