Archive for the ‘Model’ 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

Why ‘created’ and ‘modified’ fields might not save

May 28, 2007

https://trac.cakephp.org/ticket/2595

I found this when I was searching for a reason that the created and modified fields were not automagically saving when I saved a record to the database.

What is says is that instead of defining the created and modified fields as NOT NULL, they should be defined as NULL and let Cake then go ahead and put an entry into them. This is for version 1.2

Use
`modified` datetime default NULL,
instead of
`modified` datetime default NOT NULL default '0000-00-00 00:00:00',

Follow

Get every new post delivered to your Inbox.