Last active
September 26, 2015 01:46
-
-
Save rederlo/0aaa99abc503f34941ae to your computer and use it in GitHub Desktop.
CAKEPHP 2.X_custon_find
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: ederlo | |
* Date: 25/09/15 | |
* Time: 22:39 | |
*/ | |
App::uses('AppModel', 'Model') | |
class Course extends AppModel { | |
/** | |
* @var array | |
*/ | |
public $findMethods = array( | |
'rankCourseReport' => true, | |
); | |
/** | |
* @param $state | |
* @param $query | |
* @param array $results | |
* @return array | |
*/ | |
protected function _findRankCourseReport($state, $query, $results = array()) | |
{ | |
if ($state === 'before') { | |
$query['fields'] = array( | |
'Course.name', | |
'User.name', | |
'(count(CoursesAccessActivity.courses_activity_id)*100) as score' | |
); | |
$query['joins'] = array( | |
array( | |
'table' => 'courses_access_activities', | |
'alias' => 'CoursesAccessActivity', | |
'type' => 'INNER', | |
'conditions' => array('CoursesAccessActivity.course_id = Course.id'), | |
), | |
array( | |
'table' => 'users', | |
'alias' => 'User', | |
'type' => 'INNER', | |
'conditions' => array('User.id = CoursesAccessActivity.user_id'), | |
), | |
array( | |
'table' => 'courses_activities', | |
'alias' => 'CoursesActivity', | |
'type' => 'INNER', | |
'conditions' => array('CoursesActivity.id = CoursesAccessActivity.courses_activity_id'), | |
), | |
); | |
return $query; | |
} | |
return $results; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: ederlo | |
* Date: 25/09/15 | |
* Time: 22:39 | |
*/ | |
App::uses('AppController', 'Controller'); | |
/** | |
* @property mixed Course | |
* @property array paginate | |
*/ | |
class CoursesController extends AppController | |
{ | |
public function index() | |
{ | |
$this->paginate = array( | |
'rankCourseReport', | |
'limit' => 10 | |
); | |
$this->set(array( | |
'ranks' => $this->paginate('Course'), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment