Re: Paginating results of custom queries?

2007-08-02 Thread kabturek

by default paginator in 1.2 is doing a findAll with the conditions
that you give him but if that's not enough for you then you can
declare a paginate function in your model that will be used instead of
findAll  ( 
http://api.cakephp.org/1.2/libs_2controller_2controller_8php-source.html#l01100
)  you should also define a paginateCount() function in your model.
You've got the params in the API. I suggest using it :) it's a
powerfull tool.

greets,

On Aug 1, 5:19 pm, kionae <[EMAIL PROTECTED]> wrote:
> No one knows?
>
> On Jul 30, 3:48 pm, kionae <[EMAIL PROTECTED]> wrote:
>
> > Is there any way to just pass an array into 1.2's paginate function
> > and have it paginate the data?  Essentially, I have an array that is a
> > set of search results returned by some custom queries and a bit of
> > manipulation.  I couldn't generate the results I needed by going
> > through $this->paginate because it wouldn't let me join tables, so I'm
> > stuck with using the custom queries.
>
> > I've seen other topics on this, but there didn't seem to be a clear
> > answer (in fact I'm still not even sure this is possible after reading
> > them all).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Paginating results of custom queries?

2007-08-01 Thread Arjen V
I've done this a while back. It probably isn't the best way, but it works.
I'm not sure if this was done with the most recent alpha, but it's easy to
recreate: just remove the code that's used to run the findAll query and make
it use the one you supply.

cake/app_controller.php

  function paginateResultSet($object = null, $results)
  {

  if (is_array($object)) {
  $whitelist = $scope;
  $scope = $object;
  $object = null;
  }

  if (is_string($object)) {
  if (isset($this->{$object})) {
  $object = $this->{$object};
  } elseif (isset($this->{$this->modelClass}) &&
isset($this->{$this->modelClass}->{$object})) {
  $object = $this->{$this->modelClass}->{$object};
  } elseif (!empty($this->uses)) {
  for ($i = 0; $i < count($this->uses); $i++) {
  $model = $this->uses[$i];
  if (isset($this->{$model}->{$object})) {
  $object = $this->{$model}->{$object};
  break;
  }
  }
  }
  } elseif (empty($object) || $object == null) {
  if (isset($this->{$this->modelClass})) {
  $object = $this->{$this->modelClass};
  } else {
  $object = $this->{$this->uses[0]};
  }
  }

  if (!is_object($object)) {
  // Error: can't find object
  return array();
  }
  $options = am($this->params, $this->params['url'], $this->passedArgs);

  if (isset($this->paginate[$object->name])) {
  $defaults = $this->paginate[$object->name];
  } else {
  $defaults = $this->paginate;
  }

  if (isset($options['show'])) {
  $options['limit'] = $options['show'];
  }

  if (isset($options['sort']) && isset($options['direction'])) {
  $options['order'] = array($options['sort'] =>
$options['direction']);
  } elseif (isset($options['sort'])) {
  $options['order'] = array($options['sort'] => 'asc');
  }

  if (!empty($options['order']) && is_array($options['order'])) {
  $key = key($options['order']);
  if (strpos($key, '.') === false && $object->hasField($key)) {
  $options['order'][$object->name . '.' . $key] =
$options['order'][$key];
  unset($options['order'][$key]);
  }
  }

  $vars = array('fields', 'order', 'limit', 'page', 'recursive');
  $keys = array_keys($options);
  $count = count($keys);

  for($i = 0; $i < $count; $i++) {
  if (!in_array($keys[$i], $vars)) {
  unset($options[$keys[$i]]);
  }
  if (empty($whitelist) && ($keys[$i] == 'fields' || $keys[$i] ==
'recursive')) {
  unset($options[$keys[$i]]);
  } elseif (!empty($whitelist) && !in_array($keys[$i], $whitelist))
{
  unset($options[$keys[$i]]);
  }
  }

  $conditions = $fields = $order = $limit = $page = $recursive = null;
  if (!isset($defaults['conditions'])) {
  $defaults['conditions'] = array();
  }

  extract($options = am(array('page' => 1, 'limit' => 20), $defaults,
$options));
  /*if (is_array($scope) && !empty($scope)) {
  $conditions = am($conditions, $scope);
  } elseif (is_string($scope)) {
  $conditions = array($conditions, $scope);
  }
  $recursive = $object->recursive;
  */
  $count = $object->findCount($conditions, $recursive);
  $pageCount = ceil($count / $limit);

  if($page == 'last') {
  $options['page'] = $page = $pageCount;
  }

  //$results = $object->findAll($conditions, $fields, $order, $limit,
$page, $recursive);
  $paging = array(
  'page'=> $page,
  'current'=> count($results),
  'count'=> $count,
  'prevPage'=> ($page > 1),
  'nextPage'=> ($count > ($page * $limit)),
  'pageCount'=> $pageCount,
  'defaults'=> am(array('limit' => 20, 'step' => 1), $defaults),

  'options'=> $options
  );

  $this->params['paging'][$object->name] = $paging;

  if (!in_array('Paginator', $this->helpers) &&
!array_key_exists('Paginator', $this->helpers)) {
  $this->helpers[] = 'Paginator';
  }

  return $results;
  }


Arjen

On 8/1/07, Ketan Patel <[EMAIL PROTECTED]> wrote:
>
>
> You will have to overload the paginate method in your model. And then
> in the model you could write your custom queries as required. Read the
> code for more information.
>
> Ketan.
>
> kionae wrote:
> > Is there any way to just pass an array into 1.2's paginate function
> > and have it paginate the data?  Essentially, I have an array that is a
> > set of search results returned by some custom queries and a bit of
> > manipulation.  I couldn't generate the results I needed by going
> > through $this

Re: Paginating results of custom queries?

2007-08-01 Thread Ketan Patel

You will have to overload the paginate method in your model. And then
in the model you could write your custom queries as required. Read the
code for more information.

Ketan.

kionae wrote:
> Is there any way to just pass an array into 1.2's paginate function
> and have it paginate the data?  Essentially, I have an array that is a
> set of search results returned by some custom queries and a bit of
> manipulation.  I couldn't generate the results I needed by going
> through $this->paginate because it wouldn't let me join tables, so I'm
> stuck with using the custom queries.
>
> I've seen other topics on this, but there didn't seem to be a clear
> answer (in fact I'm still not even sure this is possible after reading
> them all).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Paginating results of custom queries?

2007-08-01 Thread rtconner

Just found this link, give it a read also, maybe it can help you.
http://groups.google.com/group/cake-php/browse_frm/thread/8cbf01f7a9acda57/9e84f7aacbf3cd85?#9e84f7aacbf3cd85

On Aug 1, 9:32 am, rtconner <[EMAIL PROTECTED]> wrote:
> Yeah I'm doubting anyone has a good answer for you.
>
> "Is there any way to just pass an array into 1.2's paginate function
> and have it paginate the data?"
>
> No. Sorry.
>
> If you work hard enough, likely I'm thinking you can join the tables
> you want to join. I came across this once, and I kind of ended up
> writing a "fake_paginate" method to do it. Not a great idea. Another
> thing, I've never tried, but have been meaning to try is this: In your
> relationship definitions in your models.. I'm theorizing you can write
> some combination of finderQuery and counterQuery and then paginate
> will use them and happily work. These are area I've never really
> explored so really I have no idea if they'll work. It's just a theory
> I have.
>
> -Rob Conner
>
> On Aug 1, 9:19 am, kionae <[EMAIL PROTECTED]> wrote:
>
> > No one knows?
>
> > On Jul 30, 3:48 pm, kionae <[EMAIL PROTECTED]> wrote:
>
> > > Is there any way to just pass an array into 1.2's paginate function
> > > and have it paginate the data?  Essentially, I have an array that is a
> > > set of search results returned by some custom queries and a bit of
> > > manipulation.  I couldn't generate the results I needed by going
> > > through $this->paginate because it wouldn't let me join tables, so I'm
> > > stuck with using the custom queries.
>
> > > I've seen other topics on this, but there didn't seem to be a clear
> > > answer (in fact I'm still not even sure this is possible after reading
> > > them all).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Paginating results of custom queries?

2007-08-01 Thread rtconner

Yeah I'm doubting anyone has a good answer for you.

"Is there any way to just pass an array into 1.2's paginate function
and have it paginate the data?"

No. Sorry.

If you work hard enough, likely I'm thinking you can join the tables
you want to join. I came across this once, and I kind of ended up
writing a "fake_paginate" method to do it. Not a great idea. Another
thing, I've never tried, but have been meaning to try is this: In your
relationship definitions in your models.. I'm theorizing you can write
some combination of finderQuery and counterQuery and then paginate
will use them and happily work. These are area I've never really
explored so really I have no idea if they'll work. It's just a theory
I have.

-Rob Conner

On Aug 1, 9:19 am, kionae <[EMAIL PROTECTED]> wrote:
> No one knows?
>
> On Jul 30, 3:48 pm, kionae <[EMAIL PROTECTED]> wrote:
>
> > Is there any way to just pass an array into 1.2's paginate function
> > and have it paginate the data?  Essentially, I have an array that is a
> > set of search results returned by some custom queries and a bit of
> > manipulation.  I couldn't generate the results I needed by going
> > through $this->paginate because it wouldn't let me join tables, so I'm
> > stuck with using the custom queries.
>
> > I've seen other topics on this, but there didn't seem to be a clear
> > answer (in fact I'm still not even sure this is possible after reading
> > them all).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Paginating results of custom queries?

2007-08-01 Thread kionae

No one knows?

On Jul 30, 3:48 pm, kionae <[EMAIL PROTECTED]> wrote:
> Is there any way to just pass an array into 1.2's paginate function
> and have it paginate the data?  Essentially, I have an array that is a
> set of search results returned by some custom queries and a bit of
> manipulation.  I couldn't generate the results I needed by going
> through $this->paginate because it wouldn't let me join tables, so I'm
> stuck with using the custom queries.
>
> I've seen other topics on this, but there didn't seem to be a clear
> answer (in fact I'm still not even sure this is possible after reading
> them all).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---