On Sun, Jan 23, 2011 at 11:16 AM, mmamedov <[email protected]> wrote:
> OK. This is getting weird.
> Now it works for custom query, but it doesn't work when I need default
> pagination for other actions of the same controller.
>
> The problem is however, with the overriding paginate() and
> paginateCount() - Cake 1.3 manual and the source code of both, are
> different.
>
> In Cake manual, http://book.cakephp.org/view/1237/Custom-Query-Pagination,
> it says:
>       function paginate($conditions, $fields, $order, $limit, $page =
> 1, $recursive = null, $extra = array()) { ... }
> But in the source code I see:
>       function paginate($object = null, $scope = array(), $whitelist
> = array()) { ...}
>
> So which one is it? And I also seem unable to find paginateCount()
> definition in source code (At least that's what Netbean's Find is
> uinable to find)

You're looking at the wrong class. Controller has a paginate() method,
but Model::paginate() will be called if it exists. Sorry, it's not
precise to say that you're overriding the method in the model. Better
to say that you're including it. Look at Controller::paginate again.
http://api.cakephp.org/view_source/controller/#line-1052

Line 1181 is where $scope gets folded into $conditions. Line 1197 is
where it checks if your model has a paginateCount() and calls it if
so. Line 1215 is where Model::paginate() is looked for.



> BTW, this is how I defined them in my model:
>
>     function paginate($conditions=null, $fields=null, $order=null,
> $limit=null, $page = 1, $recursive = null, $extra = array()) {
>             if (isset($conditions['sql']))
>             {
>                 $recursive = -1;
>                 $query = $conditions['sql'];
>                 unset($conditions['sql']);
>                $query .= ' LIMIT '.($page-1)*$limit.','.$limit;
>
>                 return $this->query($query);
>             }
>             else{
>                 return parent::paginate($conditions, $fields, $order,
> $limit, $page,$recursive, $extra);
>             }
>
>        }
>        function paginateCount($conditions = null, $recursive = 0,
> $extra = array()) {
>            if (isset($conditions['sql']))
>            {
>                $sql = $this->mmPaginateCount; //I assign this from
> controller, i need it this way.
>                $results = $this->query($sql);
>                return $results[0][0]['c'];
>            }
>            else{
>               return  parent::paginateCount($conditions, $recursive,
> $extra);
>            }
>
>        }
>
> And this is how I settup custom plain sql from controller before
> calling $this->paginate();
>
> $this->paginate=array(
>    'limit'=>Configure::read('Page.PostCount') //my appwide limit for
> posts
>    ,'conditions'=>array(
>           'sql' =>"my custom query"));
>
>
> Any thoughts?

If you're defining $paginate as a class var (outside of the action)
you could instead just change certain keys in $this->paginate, rather
than re-defining the entire array. Like limit, for example.

Also, the conditions could be defined within Model::paginate. If you
need to pass certain values you could do so in the $scope array in the
controller. As in your example:

$scope = array('union' => array('Friend.user_id' => 99));

$data = $this->paginate('hatever', $scope);

In the model:

if (exists($scope['union']))
{
    $conditions = array_merge($conditions, $scope['union']);

This allows you to pass several params within one. You could even do
this in the controller:

$scope = array(
        'union' => array(
                'conditions' => array(
                        'Friend.user_id' => 99
                ),
                'limit' => 10,
                'order' => array(
                        'Friend.last_name' => 'ASC'
                )
        )
);

And then sort it out as necessary inside the model.

The 'union' key is arbitrary. It could be 'foo' or whatever.

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to