Wednesday, March 28, 2012

Re: count from different model in find by id

I ended up doing the following which works but I do wonder is there a better cakePHP way.

Thanks

REPORTS CONTROLLER

        // LOAD REQUIRED MODELS
        // --------------------------------------------------------------->
        $this->loadModel('User');
        $this->loadModel('Task');


        // SELECT STAFF MEMEBRS
        // --------------------------------------------------------------->
        $staffmembers = $this->User->find('all', array(
                        'fields' => array('User.id', 'User.first_name', 'User.last_name'),
                        'conditions' => array('User.group_id' => 3, 'User.active' => 1),
                        'recursive' => 0
        ));
        $this->set('staffmembers', $staffmembers);
       
       
        // FOR EACH STAFF MEMEBR WORK OUT THE COUNT INTO AN ARRAY
        // ---------------------------------------------------------------------------->
        foreach ($staffmembers as $staff)
        {
            $currenttasks[] = $this->Task->find('count', array('conditions' => array('Task.completed' => 0, 'Task.user_id' => $staff['User']['id'], 'Task.due_date >' => $thedate)));
            $completedtasks[] = $this->Task->find('count', array('conditions' => array('Task.completed' => 1, 'Task.user_id' => $staff['User']['id'])));
            $overduetasks[] = $this->Task->find('count', array('conditions' => array('Task.completed' => 0, 'Task.user_id' => $staff['User']['id'], 'Task.due_date <' => $thedate)));
       
            $this->set('currenttasks', $currenttasks);
            $this->set('completedtasks', $completedtasks);
            $this->set('overduetasks', $overduetasks);
        }

REPORT TASK VIEW
        $i = 0;
        foreach ($staffmembers as $row)
        {
            $thefullname = $row['User']['first_name'] . ' ' . $row['User']['last_name'];
            $theuserid = $row['User']['id'];
               echo "data.addRow(['".$thefullname."', ".$currenttasks[$i].", ".$completedtasks[$i].", ".$overduetasks[$i]."]);";
            $i++;
        }


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: