Feed items

Customizing mysql query of a bound model

I had a hasAndBelongsToMany binding in my Charity Model as i normally would need all the “Causes” available along with Charity information and vice-versa. At one place i needed a specific number of “Causes” picked randomly from the database table. To make this work i had to add following two lines just before the $this->Charity->find call in my controller action:

$this->Charity->hasAndBelongsToMany['Cause']['limit']=5;
$this->Charity->hasAndBelongsToMany['Cause']['order']='rand()';

and i was done.
Does someone has a better solution? Please suggest.

Creating Ajax form in Cakephp

With the help of prototype/JavaScript we can build really cool and fully functional ajax form in CakePHP really fast.
I have been using the ajax forms in CakePHP for a long time now but in the view of continuous development work always on at CakePHP framework development community i have had to alter ways to write the same code. So here is the latest one.
Prior to Cakephp 1.2 i had been using

<?php echo $ajax->form(
'search_retailers','POST',
array('update' => 'retailersListID',
'loading'=>"Element.show('plsLoaderID')",
'loaded'=>"Element.hide('plsLoaderID')",
'before'=>""))?>

to create a ajax form request element but when i tried to update my old site to latest 1.3 version of cakephp it would not work anymore. The latest way to create a ajax form element had turned to something like this:

More shorter way to load models on the fly

In earlier versions of CakePHP we had loadModel function to load model on the fly. There had been other correspondents like loadHelper to load helpers and so on. Later by the coming of 1.2 it turned into App::import(’Model’, ‘model_name’). By using later App::import() we can load core libraries, controllers, models, helpers, components and behaviors on the fly whenever needed. Following the syntax of import method but in most cases import uses only first two arrangements though:

App::import($type, $name, $parent, $search, $file, $return);

Although we use the same import method to load these classes (models, helpers etc.) they may look to use slightly different approach to construct or create class objects. In fact they simply use the class name as always to create an object. For example to load a model “User” (class name is User) we use:

Disable CakePHP debug output when using Ajax

While developing a CakePHP application and debug value set to greater than 0(zero) (development mode) we may want to show our Ajax results clean i.e. without debug information (in form of SQL log etc.) attached to it.
To make the Ajax result clean we may add a few lines of code in our controller’s (AppController in order to take effect globally) beforeFilter function which is resulted to show like the following (In case of AppContoller):

What do you hate about CakePHP?

After looking at an interesting Google group topic
Poll: what do you hate about CakePHP? i have tried to sum up the facts which people reasoned for hating/disliking CakePHP. Please be noted that all the facts listed here are based on users postings in the very topic and I have not tried to reproduce/test them all in a working environment. So, please take them home with your own consent. At least have a look at this topic and follow other links / facts behind the reason for such posting of users.
Here are the so called shortcomings of CakePHP:

Useful string manipulation helpers in CakePHP

CakePHP comes with a bunch of useful helpers and components which provide handy tools for processing and manipulating data. These helpers are not only the valuable assets for the a framework user, but can be of great help to someone writing a core php application and not using the CakePHP framework.
Here are a few examples:
Number Helper
Number helper provides useful function to manipulate numbers.
precision()
precision() returns a number formatted with a level of precision. For example:

echo $number->precision(25.02558, 3);
//outputs
25.026

toReadableSize()
This function takes filesize as a argument and return the well formatted string mentioning the size in terms of bytes, KB, MB, GB and TB. For example:

getid3 component for cakephp

What is getID3?
getID3() is an open-source, cross-platform software library for the PHP language written by James Heinrich and Allan Hansen. getID3() can extract information from multimedia file formats: audio, video and images in various formats. Information extracted includes playtime, metadata, bitrate, sample rate, number of channels, and much more. getID3() can also write metadata (tags) to several formats.
Component for CakePHP
Its very straight forward to use getid3() in cakephp and this article describes how i used it in my cakephp application. I downloaded getid3() library, extracted it to vendors directory and created a component named getid3. Then i used it in my controller class to extract the media file information.

Passed arguments routing and custom ajax pagination url in cakephp

While working in my recent “charity project” i had to list all charities found in a particular region or state. I created a regions_controller and an index function in it to fetch and set records and index.ctp as a view page to display HTML results. As normal, it had to be a http://givebackindia/regions/index call to process the page, but, i wanted to look it like http://givebackindia/region/punjab in the browser address bar so i added a “route elements” routing in my app/config/routes.php file as below:

An alternate to paginator counter showing page in cakephp

An ordinary yet useful piece of hack to the way the cakephp pagination’s pages “showing page” parameter is displayed. As default, we are used to use $paginator->counter() method which gives us a counter string for the paged result set like “Showing page 1 of 3″. But in my latest project task i needed to show results set something like “Showing 1 to 20 of total 46″.
To display the result set like this, i wrote a few lines of code in my view using $paginator object values. Here are the lines of code which i place right under the $paginator->options() call:

Cakephp important facts you must know - Part 2

CakePHP comes with a bunch of useful helpers and components which provide user handy tools to process and manipulate data. These helpers are not only the valuable assets for the framework user, but can be of great help to someone writing a core php application and not using the CakePHP framework.
Here are a few examples:
Number Helper
Number helper provides useful function to manipulate numbers.
precision()
precision() returns a number formatted with a level of precision. For example:

echo $number->precision(25.02558, 3);
//outputs
25.026

toReadableSize()
This function takes filesize as a argument and return the well formatted string mentioning the size in terms of bytes, KB, MB, GB and TB. For example: