0

I am new to cake so please be patient. I have this array

Array
(
[0] => Array
    (
        [CoursesLessons] => Array
            (
                [id] => 108
                [course_id] => 88
                [lesson_id] => 6
                [deleted] => 
            )

        [UserLessonsCompleted] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [courses_lessons_id] => 108
                        [user_id] => 75
                        [created] => 2015-01-27 14:04:31
                        [modified] => 0000-00-00 00:00:00
                        [completed_at] => 2015-02-03 14:04:45
                        [homework_downloaded_at] => 0000-00-00 00:00:00
                        [deleted] => 
                    )

            )

    )

 //and so on

I would like to exctract the UserLessonsCompleted completed at. What I am trying to do is:

$stringOfIdsCompleted =  Set::extract($coursesLessons, '{n}.UserLessonsCompleted.{n}.completed_at');

my problem is that they array that is returned is:

Array
(
 [0] => Array
    (
        [0] => 2015-02-03 14:04:45
    )

[1] => Array
    (
        [0] => 2015-02-06 15:08:01
    )
//and so on

while I would like something as:

 Array
 (
 [0] => Array
    (
        [completed_at] => 2015-02-03 14:04:45
    )

[1] => Array
    (
        [completed_at] => 2015-02-06 15:08:01
    )

Any help on how to achieve this? thank you in advance for help

here is how i retrieve my data:

$coursesLessons = $this->CoursesLessons->find('all', array(
        'conditions' => array(
            'CoursesLessons.course_id' => $courseId,
        ),
        'contain' => array(
            'UserLessonsCompleted' => array(
                'conditions' => array(
                    'UserLessonsCompleted.user_id' => $userId,
                ),
            ), 
        )
    ));

2 Answers 2

2

In your find method add fields property, like:

$coursesLessons = $this->CoursesLessons->find('all', array(
    'conditions' => array(
        'CoursesLessons.course_id' => $courseId,
    ),
    'contain' => array(
        'UserLessonsCompleted' => array(
            'fields' => array('UserLessonsCompleted.created_at'),
            'conditions' => array(
                'UserLessonsCompleted.user_id' => $userId,
            ),
        ), 
    )
));

then extract:

$stringOfIdsCompleted =  Set::extract($coursesLessons, '{n}.UserLessonsCompleted');
Sign up to request clarification or add additional context in comments.

Comments

0

The best way of doing this would be to handle it in the initial find in your controller / model using either the fields or the contain option, something like this:

$this->CoursesLesson->find('all', 
    array('contain' => array('UserLessonsCompleted.created_at'))
);

You will need to ensure that you have the "containable" behaviour attached (I attach it to my AppModel as I use it all the time).

If you cant work this out post your controller code and I will be able to give you a more tailored answer :)

2 Comments

hi thank you for your answer, see edited question for how I am retrieving the data
@Salines answer below is bang on :)

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.