Are you running the find() on the User or Attendance model? It looks
to me like it's the former.

Anyway, you could tighten this up a little by putting some code in the
model, and by adding a helper function to bootstrap.php:

function dateStr($a)
{
        if (!empty($a['year']) && !empty($a['month']) && !empty($a['day']))
        {
                return "${a['year']}-${a['month']}-${a['day']}";
        }
        return null;
}

Also, you can put the school_id in the forma as well, instead of
passing it through the session.

public function admin_attendance_by_date_range()
{
        $data = $this->User->attendanceByDateRange(
                $this->data['Attendance']['school_id'],
                dateStr($this->data['Attendance']['start_date']),
                dateStr($this->data['Attendance']['end_date'])
        );
        
        ...
}

User model:

public function attendanceByDateRange($school_id, $from, $to)
{
        return $this->Attendance->find(
                'all',
                array(
                        'conditions' => array(
                                'Attendance.created BETWEEN ? AND ?' => 
array($from, $to),
                                'Attendance.school_id' => $school_id
                        ),
                        'contain' => array(
                                'User' => array(
                                        'fields' => array(
                                                'User.id',                      
                        
                                                // other User fields you want
                                        )
                                )
                        )
                )
        );
}

On Wed, Aug 29, 2012 at 2:16 AM, rockbust <rockb...@optonline.net> wrote:
> If anyone can point me in the right direction.. It will be much appreciated
>
> I am trying to return a list of User where Attendance.created is between a
> date specified in my form.
> Further Attendance.created needs to be equal or greater than
> attendance_count input from the form.
> Attendance belongsTo User
>
> I have tried this but get sql error Unknown column 'Attendance.created'
>
>         function admin_attendance_by_date_range() {
>                 $this->Attendance->recursive = 0;
>                 $session_school_id = 
> $this->Session->read('Userinfo.currentSchoolid');
>
>                         $from =  
> $this->data['Attendance']['start_date']['year'] . "-" .
> $this->data['Attendance']['start_date']['month'] . "-" .
> $this->data['Attendance']['start_date']['day'];
>                         $to =  $this->data['Attendance']['end_date']['year'] 
> . "-" .
> $this->data['Attendance']['end_date']['month'] . "-" .
> $this->data['Attendance']['end_date']['day'];
>                         $cond = array('Attendance.created BETWEEN ? AND ?' => 
> array($from, $to),
> 'Attendance.school_id' => $session_school_id);
>
> Robert
>
>
>
>
> --
> View this message in context: 
> http://cakephp.1045679.n5.nabble.com/complex-find-tp5710758.html
> Sent from the CakePHP mailing list archive at Nabble.com.
>
> --
> 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+unsubscr...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>

-- 
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+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply via email to