Archive for the ‘Controller’ Category

h1

Containable in CakePHP

May 26, 2009

I’ve see a lot of traffic to my post about Using unBindModel in CakePHP and while it is a way of cutting down the data that is returned from a query, there is now a better way. Containable behaviour

I’ve only just stumbled upon this and after having used it briefly in testing it seems like a really cool way to chain together models and yet only get the data that you want at the time.

I have used it like this to get the County that a Club is in

$this->User->contain(array(
            'Club' => array(
                'County'
            )
        ));
        $user = $this->User->find('first', array(
            'conditions' => array(
                'User.id' => 1
            ),
        ));

This query brings back an array with User data, Club data and County data. The interesting bit is the Club

[Club] => Array
        (
            [id] => 1
            [name] => Ballymun Kickhams GAA Club
            [county_id] => 2
            [County] => Array
                (
                    [id] => 2
                    [province_id] => 1
                    [name] => West Dublin
                )

        )
h1

If you have problems with beforeFilter

September 3, 2007

I was having some problems trying to use the beforeFilter function in my controllers because I was using it in app_controller.php as well. In app_controller.php I was playing with the session variables and this was breaking when I went to use the beforeFilter function in any of my controllers. The workaround is very simple and came from the mailing list.
In you controller version of the beforeFilter function, add in the following line:
parent::beforeFilter();
before you do anything in the function. This explicitly calls the beforeFilter function in app_controller.php and runs it first.

Thanks to Jon Bennet for that little nugget

h1

Drag and Drop ordering

June 29, 2007

Here is a very useful link that walks you through how to create a simple drag and drop section for a website. It creates a list and lets you move items around the list so that you can order them as you wish. With a little bit of CSS, this could turn into a really nice simple organiser.

http://dustinweber.com/cakephp/cakephp-sortable-ajax-drag-drops-the-basics/

I did find that I had to edit my javascript includes a little bit though. I had the scriptaculous libraries loaded with this tagged on at the end
?load=effects
With this in place, I was getting errors that sortable wasn’t defined. Once I removed it, it all worked perfectly.

h1

Using unBindModel() in CakePHP

June 15, 2007

If you have a lot of associations in your model setups, sometimes you dont want all that information to be returned in a query. The answer to this is to use the unBinModel() function. Using this you can unbind certain parts of your associations.
The easiest way to demonstrate this is with an example.

If you have a user model with the following associations:

var $hasMany = array(
'Worksheet' => array('className' => 'Worksheet'
),
'Login' => array('className' => 'Login'
),
'Expense' => array('className' => 'Expense'
)
);
var $belongsTo = array(
'Level' =>
array('className' => 'Level'),
'County' =>
array('className' => 'County'),
'Province' =>
array('className' => 'Province')
);

That is potentially a lot of information to get back each time you query the User.
So if we define

$this->User->unBindModel(array(hasMany => array('Login', 'Expense')));

before we call

$this->User->findAll();

we no longer return information from the Login and Expenses tables. This can be used for any of the associations and can cut down query times dramatically.

h1

A simple way to order results in a pagination query

May 29, 2007

In the controller, put this piece of code:


$this->paginate = array('order' => 'Topic.created DESC');
$this->set('topics', $this->paginate(array('Topic.board_id' => $id)));

h1

How to login and then redirect to the last page that was looked at

May 28, 2007

In the function checkSession which is in the appController


function checkSession() {
// If the session info hasn't been set...
if (!$this->Session->check('User')) {
$this->Session->write('login_referrer',$this->params['url']['url']);
// Force the user to login
$this->redirect('/users/login');
exit();
}
}

And in the login() function in the user controller we place the follow code:


if ($success) {
if ($this->Session->check(’login_referrer’)) {
$loginReferrer = $this->Session->read(’login_referrer’);
$this->Session->delete(’login_referrer’);
$this->redirect($loginReferrer);
return true;
}
// the line before is the original redirect code
$this->dAuth->redirect(’login_success’);
return true;
}