CakePHP :: The Threaded type

After going through the neighbors and list types of $model->find method, we finally look at the "threaded" type. We fetch the threaded type result in a similar fashion as other types:

$model->find("threaded", $conditions);

So when and why would you need it? You'd want to use the threaded type when retrieving any records that have a parent-child relationship. For example in forums or comments you may begin a topic based on subject and find all responses to that particular topic which would essentially be the children of the original topic. And that relationship could be recursive, in the sense, that you may have further children within the topic for a sub-topic. Most comment forums use these kinds of recursive threads.

CakePHP SQL Lists

As promised, we continue the discussion on how to use $model->find and its various types. Earlier we looked at getting neighbors so today let's look at the "list" type. Just like neighbors, we fetch the list using the following method:

$model->find("list", $conditions);

But what is the purpose of list?. To understand that, let's look at the output from an actual find method with a "list" type.

Finding neighbors with CakePHP

There's a benefit to using CAKEPHP to handle all your SQL as opposed to you providing your own, even for complex SQL statements. First of all, you do not have to worry about SQL injection since Cake can sanitize and escape your data for you. You can use pagination with the results and its handled cleanly by the framework. Your program code is more portable and database independent since translation tables take care of specific database type differences such as BOOL vs. TINYTINT etc. Encapsulation is easier when you only have to deal with all your queries through one method - 'find'. And this method can handle most SQL type requests. It may easily be extended to handle more specific types of queries as well. Over the next few days I'll document various ways of building complex queries using pure CAKE and no SQL. Today we'll look at finding neighbors using CAKEPHP.