Refer to : http://www.littlehart.net/atthekeyboard/2008/03/04/custom-cakephp-12-pagination-queries/
These are the available options for customizing the paginate with find all
/**
* Custom paginate method
*/
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null) {
$conditions[] ="1 = 1 GROUP BY foo, bar_id, snafu_id";
$recursive = -1;
$fields = array(foo, 'away’,’ bar_id ', 'snafu_id');
$params = array(
'conditions' => $conditions,
'recursive' => $recursive,
'fields' = $fields,
'order' => $order,
'limit' => $limit,
'page' => $page
);
return $this->find('all', $params);
}
To implement this you overload the methods in your models and controllers.
You can also use the bindModel function in your controller to get the desired recursion.
It is best to use well defined models, but as you have said, you need to provide different join structures for multiple situations.
You can overload the following methods in this way:
class Teacher() extends AppModel
{
/**
* Custom paginate method
*/
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null)
{
$params = array
( 'conditions' => $conditions,
'recursive' => $recursive,
'fields' => $fields,
'group' => array('Teacher.id'),
'order' => $order,
'limit' => $limit,
'page' => $page
);
return $this->find('all', $params);
}
/**
* Custom paginateCount method
*/
function paginateCount($conditions = null, $recursive = 0)
{
$params = array
( 'conditions' => $conditions,
'recursive' => 1,
'fields' => array('Teacher.id'),
'group' => array('Teacher.id')
);
return
count
( $this->find
( 'all',
$params
)
);
}
}
class TeachersController extends AppController
{
function bind_teacher_tables()
{
$this->Teacher->bindModel(array(
'hasOne' => array(
'TeacherYear',
'Year' => array(
'className' => 'Year',
'foreignKey' => false,
'conditions' => array('Year.id = TeacherYear.year_id')))));
$this->Teacher->bindModel(array(
'hasOne' => array(
'TeacherCourse',
'Course' => array(
'className' => 'Course',
'foreignKey' => false,
'conditions' => array('Course.id = TeacherCourse.course_id')))));
$this->Teacher->bindModel(array(
'hasOne' => array(
'TeacherSkill',
'Skill' => array(
'className' => 'Skill',
'foreignKey' => false,
'conditions' => array('Skill.id = TeacherSkill.skill_id')))));
$this->Teacher->bindModel(array(
'hasOne' => array(
'TeacherArea',
'Area' => array(
'className' => 'Area',
'foreignKey' => false,
'conditions' => array('Area.id = TeacherArea.area_id')))));
$this->Teacher->bindModel(array(
'hasOne' => array(
'TeacherSubject',
'Subject' => array(
'className' => 'Subject',
'foreignKey' => false,
'conditions' => array('Subject.id = TeacherSubject.subject_id')))));
}
function search()
{
… process $this->data and build your custom $options array …
/* in my case, I’m using default field set, with the group by option set.
This method should allow you to have custom field lists, custom bindings,
and it seems to me that you could do some fancy switch logic with the
model and set different conditions for this with an overload to the AppModel
like AppModel::setPaginateType(‘my_descriptive_filter_identifier’);
to overload, copy the appModel.php from the cake directory to your app
directory and add the necessary logic.
Just be careful. You don’t want to walk on the core functionality.
Don’t change the prototype for any cake functions which already exist.
Try to only add methods which you use to customize the parameters to
the cake code in your specific model classes.
*/
$options =
( isset($conditions)
? array
( 'conditions' => $conditions
)
: array
( 'conditions' => array
( 'User.f_name <>' => 'admin' )
)
); $this->bind_teacher_tables();
$teacher_list =
$this->paginate
( ( isset($conditions)
? $conditions
: array
( 'User.f_name <>' => 'admin' )
)
);
}
Hope this helps.
Software Developer
…………………………………………………………………………………………………………………
The
¡NEW! Please check out our new Portfolio Website:
Connaxis Creative Outsourcing Specialist www.creative-outsourcing.com
First page position Google.com: Creative Outsourcing
-----Original Message-----
From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of a.b
Sent: Tuesday, December 02, 2008 3:49 PM
To: CakePHP
Subject: block fields returned by find ( 'all') or paginate
Hello,
I would like to return some fields at my requests find ( 'all').
I wish I could define the model in order not to every request, return
the fields that I would like to see return.
I have belongsto class products and categories that hasmany products.
Class I and which belongto varieties and varieties that belonsto
category.
In each table wholesale I fields id, libel, created, modified
I can not find the solution to paginate in and find ( 'all') than it
returns me that the fields id, but not libel fields created and
modified and whatever the level of recursion.
Thank you in advance for your help
A.B
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:
Post a Comment