0

I have tried many different variation, but cannot get the right structure. Maybe, you expert might what to give it a try.

What I need to do is return the therapist who may have multiple children assigned to him. It will return the children's schedules that are assigned to him.

I have tried the following Code. This is the closest I can get to the format I need.

    // Grab the therapist    
    $therapist = $this->Therapist->find('first', array('conditions' => array('Therapist.' . $this->Therapist->primaryKey => $id)));
    // Grab the child(ren) assigned to the therapist
   // Returns id as key and name as value.
    $children =  $this->Child->get_children($id);
    $schedule = array();
   // Loop through the assigned children and get the id (key).
   foreach($children as $key => $value):
        // Loop through and grab all the scheduled days and times for child(ren).
        foreach($this->Schedule->get_full_schedule($key) as $child):
            // Have the child name at the top of the array. 
            if($name != $child['Child']['child_name']):
                 $schedule[] = array_push($schedule,  array('child_name' => $child['Child']['child_name']));
                $name = $child['Child']['child_name'];
            endif;
                // Get all the scheduled days for the child and add to array.
                if($child['Schedule']['child_id'] == $child['Child']['id']):
                    $schedule[]['Schedule'] =  $child['Schedule'];
                endif;
        endforeach;
    endforeach;

Which outputs the following Array:

   Array
                (
                    [0] => Array
                        (
                           [child_name]  => John Smith
                        )

                    [1] => 1
                    [2] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 19
                                    [child_id] => 197
                                    [days] => Monday
                                    [start_time] => 17:00:00
                                    [end_time] => 22:00:00
                                )

                        )

                    [3] => Array
                        (
                            [child_name]  => Jane Smith
                        )

                    [4] => 4
                    [5] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 16
                                    [child_id] => 138
                                    [days] => Monday
                                    [start_time] => 09:00:00
                                    [end_time] => 17:00:00
                                )

                        )

                    [6] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 17
                                    [child_id] => 138
                                    [days] => Sunday
                                    [start_time] => 09:00:00
                                    [end_time] => 12:00:00
                                )

                        )

                    [7] => Array
                        (
                            [Schedule] => Array
                                (
                                    [id] => 18
                                    [child_id] => 138
                                    [days] => Tuesday
                                    [start_time] => 09:00:00
                                    [end_time] => 17:00:00
                                )

                        )

                )

What I would like is:

                Array
                (
                    [0] => Array
                        (
                           [child_name]  => John Smith
                           [0] => Array
                               (
                                [Schedule] => Array
                                    (
                                        [id] => 19
                                        [child_id] => 197
                                        [days] => Monday
                                        [start_time] => 17:00:00
                                        [end_time] => 22:00:00
                                    )
                            )
                        )

                    [1] => Array
                        (
                            [child_name]  => Jane Smith
                            [0] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 16
                                            [child_id] => 138
                                            [days] => Monday
                                            [start_time] => 09:00:00
                                            [end_time] => 17:00:00
                                        )

                                )

                            [1] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 17
                                            [child_id] => 138
                                            [days] => Sunday
                                            [start_time] => 09:00:00
                                            [end_time] => 12:00:00
                                        )

                                )

                            [2] => Array
                                (
                                    [Schedule] => Array
                                        (
                                            [id] => 18
                                            [child_id] => 138
                                            [days] => Tuesday
                                            [start_time] => 09:00:00
                                            [end_time] => 17:00:00
                                        )

                        )

                )

Any help is appreciated.

Thank, Greg

2
  • Does it have to be EXACTLY like that, or is there a way you can generalize it like.... "I want an array where each child has an array of dates" ? Commented Jul 23, 2014 at 21:40
  • No, it doesn't. I just need it in a format, so I can pass it to a view and build out a table of the schedule for each child. Commented Jul 24, 2014 at 10:43

1 Answer 1

1

Have you thought about just using Containable Behavior? Seems MUCH easier than the way you're trying to do it:

$this->Therapist->find('first', array(
    'conditions' => array(
        'Therapist.' . $this->Therapist->primaryKey => $id
    ),
    'contain' => array(
        'Child' => array(
            'Schedule'
        )
    )
));

Not only is it a lot easier, but your data should come back in an acceptable and nested format.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I tried to put something together using Containable. I was trying it in the Schedule Model, however I will give this a try. Thanks for the insight.
I tried it. Everything worked, but didn't return the schedule for each child. Sorry.
@GregFox - then there's something else wrong with your code. Bottom line, this answer is "use containable behavior". Beyond that, you might need to ask a question about why your associations are wrong, or why containable isn't working as it should for you. My guess is you didn't set recursive to -1 per the instructions, but I can't be sure without seeing all your code.
Once I fixed my relationships, it worked like a charm. Thanks.

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.