Friday, August 31, 2012

Re: complex find()

Needs cleanup and have to put in model. I had to call it on the user model. 
looped thru results and built a new array of user id's. passed array as condition to paginate.

        $from =  $this->data['User']['start_date']['year'] . "-" . $this->data['User']['start_date']['month'] . "-" . $this->data['User']['start_date']['day'];
        $to =  $this->data['User']['end_date']['year'] . "-" . $this->data['User']['end_date']['month'] . "-" . $this->data['User']['end_date']['day'];
        $cond = array('Attendance.created BETWEEN ? AND ?' => array($from, $to), 'Attendance.school_id' => $session_school_id);
        $classes_from_form =  $this->data['User']['type'];
        $user_list  = array();
        $users = $this->User->find('all', array('contain' => array('Attendance' => array('conditions' => $cond), 'Usermembership' )));
        foreach($users as $user){
            $count = 0;
            $user_id = $user['User']['id'];
            foreach($user['Attendance'] as $attendance){
                if(($attendance['user_id'] == $user_id) && (isset($user['Usermembership']))){
                    foreach($user['Usermembership'] as $membership){
                        //check for a valid membership
                        if(($membership['status'] == 1) && ($membership['end_date'] >=  date('Y-m-d'))){   
                            $count++;
                            if($count == $classes_from_form){
                                //build array of user id's
                                $user_list[]= $user['User']['id'];
                            }
                        }
                    }
                }
            }
        }
        $cond3 = array('User.id' => $user_list);
        $this->set('attendances', $this->paginate($cond3));

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

No comments: