Item lists
Item lists are groups of Item objects retrieved from the database.
Working with Items becomes a lot more powerful when in conjunction with Item lists. Items allows you to retrieve multiple Items at once from the database and work with them as you would do with a regular list.
Just like when creating single Item classes, Item lists are App classes that extend the Cherrycake's Items core class. Let's say we want to create an Item list class to work with lists of movies. To do so, so we create the Movies
class in the file /classes/Movies.class.php
, and it looks like this:
Just by setting these two properties we'll have a working Movies
class:
tableName
The name of the table where the items are stored.itemClassName
The name of the Item class.
You can also set some other properties if you need to change the defaults:
databaseProviderName
The database provider name where this items are stored. Defaults tomain
We're now ready to start retrieving Movie lists from the database. Let's see how we could simply get a list of all the movies on the database:
You can automatically fill your Movies
object with Movie
items when creating it by passing the fillMethod
key as you see in the example above.
Since we're not specifying any parameters in the
p
key, we'll simply get all movies in the database at once.
See this example working in the Cherrycake documentation examples site.
Let's see how we could iterate through the results to show all the movie titles and their release years:
See this example working in the Cherrycake documentation examples site.
Limit results
To get only the first n
results instead of all of them, you can specify the limit
key when creating your Movies
object, like this:
Results pagination
There's also a simple way of paginating results by specifying the itemsPerPage
and page
keys. Let's say you're dividing your movie listing in pages containing five movies each, and you want to get the third page. You would do it like this:
Remember that pages start at zero, not at 1.
Iterating Items in a pattern
Since we already learned how to pass variables to a pattern, why don't we pass the $movies
object to a pattern, and iterate it there to create a nice <UL>
list? This is how it can be done:
We output the pattern using the Patterns::out method, passing the $movies
variable along:
We create the pattern MoviesList.html
like this:
And this is the result:
Alien (1979)
The Thing (1982)
Silent Running (1972)
Arrival (2016)
Interstellar (2014)
See this example working in the Cherrycake documentation examples site.
Last updated