Select Page

Educational Cake

This month the Engineers at Pollenizer sat together through an online CakePHP training program. It was a good opportunity to gauge what the CakePHP community consider advanced and to explore familiar topics from another Engineer’s perspective. Here are a few things that I found interesting

Kevin, Simon and Tom

Pagination

Pagination is the process of splitting information into distinct pages. If you’ve ever tried this task with straight PHP, I’m confident you would agree this can get messy quickly. Luckily, pagination in CakePHP is easy and the challenges only start to come into play when the query that drives the process has clauses such as GROUP BY. The answer is to use a custom find method, then pass settings options to be the name of the custom find and to override the paginationCount method in the model which is being paginated. If you’re not using CakePHP 2.2+ then you will need to copy the paginateCount as it has recently been patched to use the custom find method to count the right number of rows.

You’ll end up with something like this:

app/Controller/UsersController.php

public $components = array(
    'paginate' => array(
        'settings' => 'withCompany'
    )
);
app/Model/User.php

public function _findWithCompany()
{
    return $this->find('all', array(
        'contain' => array(
            'Company'
        )
    );
}

Time Internationalization

CakePHP supports Time Internationalization! Similar to strings, time can be formatted using CakePHP’s Time Helper. The contents will be sourced from LC_TIME which should be stored in LC_MESSAGES.

Example:

echo $this->Time->format('%x');

In the end, I would highly recommend CakePHP training courses to new and experienced users as, even as experienced users of CakePHP, we were exposed to new ways of solving programming tasks that are the fundamental building blocks to any web application.

Share This