Paginate? Recursive? What are they doing?

Avast,

One tutorial I used has this for the index() method:


$this->User->recursive = 0;
$this->set('users', $this->paginate());

which i was a little unsure what to do with. Ok, i get that I can now access, from my index.ctp, a $users variable, that has an array of results from the db. But I don't really know *what* that array really is.

Recursive, from the api, limits how far down the chain of relevant data we go. So, -1 only gets group data, 0 gets group and domain, etc

(http://book.cakephp.org/view/439/recursive)

First off, when we say (they say, anyway) "group data" is that an acl thing? And what is "domain"?

Next, what is paginate really doing? Is it just a glorified "find" that i have to use "limit" with to make any sense of? And find is, itself, just a glorified "select" on the relevant models table?

Yeah i know some coding could answer this, but i'm still fiddling a bit with cake (and php!), so i'm not, as it happens, able to really make a shot of working out what the data is doing. Any help is awesome-o!

cheers,

andrew

Asked by aiyer, on 11/1/10

1 Answer

So first off recursive. Basically, recursive determines how many "levels" of associated data you want to return with a find call.

For example lets say you have the following

User belongsTo Group

Group belongsTo Domain

So every user can be linked to one specific Group model record, and every Group can be linked to one specific Domain record.

eg.


User
-----
id: 502
username: john@doe.com
password: AFED082453A
group_id: 1

Group
-----
id: 1
name: Staff
domain_id: 2

Domain
-----
id: 2
name: mydomain.com

So recursive = 0 brings back just the User. 1 brings back User, Group and 2 brings back User, Group and Domain. Nothing to do with ACL - these could be User, Profile, Location instead of User Group Domain.

If you're using 1.2+ then I highly recommend using the Containable behavior instead of/in addition to the recursive directive.

Paginate is a like a "glorified" find() but it does have some nice features that allow you to paginate quickly and offer things like sorting by headers, paging navigation and other things really quickly in the views.

Find could be construed as a "glorified" select but really it's a data abstraction layer that allows you to swap out any data source under your model, so it's not necessarily just "select" on a mysql db. It offers automatic linking of associated models as well in a consistent format.

Answered by davidwuon 11/1/10

Champion, thanks mate!

aiyer - on 12/1/10

<< comments | comments >>
<< previous next >>

Tagged with

Rating

0

Viewed

442 times

Last Activity

on 12/1/10