Home

Thu, Apr. 24th, 2008, 12:30 am

OK, so I've been hunting all evening for something that's been in front of my face the entire time.
All I wanted to do was paginate() the result of a specific query... THAT'S MY PROBLEM. I was asking the wrong question. I wanted to paginate the result of a particular condition.
I went as far as creating the specific paginate query in the model (which is where complex paginate queries go, not the controller). Then I asked myself, if I can pass it a $condition, why can't I just do that in the controller with the default paginate().
THEN IT HIT ME! $this->paginate is a fancy kind of Model::findAll(). The same options you can give findAll() you can give to paginate().
OK, so...
  1. You can set some defaults to $this->paginate() as a 'var' in the controller called 'paginate'. (See previous post) This will apply to all vanilla $this->paginate() calls.
  2. $this->paginate() returns what $this->Post->findAll() would return.
    1. It also accepts all options findAll() would accept.
    2. Use it as you would findAll(): http://book.cakephp.org/view/448/findall
Putting it all together, five hours ago, in my "non index action" all I needed to do was:

$conditions = array('Journal.incident_id' => $incidentId);

// $journals = $this->Journal->findAll($conditions);
$journals = $this->paginate($conditions);
$this->set('journals', $journals);
You know, for all the time CakePHP is 'saving' me by auto-generating this code, it's taking back by lack of cohesive well written documentation. Does that mean I give up... ugh, dzang, no... when I 'get' it and it works... it REALLY WORKS! :)

I just wish I had more time. I have to get this site done by May 15.

Wed, Apr. 23rd, 2008, 10:28 pm
$paginate fields

In cake 1.2 when setting the "displayable" fields of the model in question, use the related model's name.
class JournalController extends AppController {
...
var $paginate = array(
'Journal' => array(
'limit' => 25,
'fields' => array(
'Journal.id',
'Incident.id',
...

Not 'Journal.incident_id'