Archive for May, 2009

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

Flash Chart Helper

May 25, 2009

I’ve been using the Flash Chart Helper recently and it’s been working really well once I got a couple of things ironed out with it.

I followed the instructions and copied all the files to the right place. I had the array created and sending what I thought was the correct format of data to be graphed but I was still getting errors. Originally I had this line:

echo $flashChart->begin(array('prototype'=>true));

at the top of my graph. When I removed the option array (because I wasn’t using prototype) it gave me a lovely graph.

Next thing was add labels to the X-axis. My data set was being sent in the following format:

Array ( 
[0] => Array ( 
    [day] => Monday 
    [hits] => 0 ) 
[1] => Array ( 
    [day] => Tuesday 
    [hits] => 15 ) 
[2] => Array ( 
    [day] => Wednesday 
    [hits] => 58 ) 
[3] => Array ( 
    [day] => Thursday 
    [hits] => 23 ) 
[4] => Array ( 
    [day] => Friday 
    [hits] => 23 ) 
[5] => Array ( 
    [day] => Saturday 
    [hits] => 99 ) 
[6] => Array ( 
    [day] => Sunday 
    [hits] => 34 
) )

So I set up

echo $flashChart->setData($logs, '{n}.hits', '{n}.day', 'byDay');
echo $flashChart->axis('x', array('labels' => $logs));

To explain that, $logs is the array of data, {n}.hits is the data to be graphed, {n}.day is the label path, byDay is the name of the graph. The options for the axis are simply the array to pass in to read the labels from.

I then wanted the labels to be vertical so that they wouldn’t overlap. This proved to be a harder thing to fix. In the API on page three of the helper article, it only mentions

axis($axis, $options = array())

I added in the option of

'vertical' => true
but I got an error that the helper couldn’t find the set_vertical() function. After some digging around in the source, I found that the real way to pass label options was as a third variable to the axis function. So I edited my call to read
echo $flashChart->axis('x', array('labels' => $logs), array('vertical' => true);

and lo and behold, vertical labels!

h1

DebugKit

May 17, 2009

Wow, this is pretty awesome!
I just downloaded DebugKit and installed it in a project I’m working on (more on that later).

It’s a nifty little plugin that gives you details about what queries are being called, what variables are being used, how long the page takes to render and a whole heap of other information. It should be pretty useful to debug some problems that I am inevitably going to run into.

I was unable to clone the repository from thechaw.com due to permissions being denied. I think you might have to sign up to it to be able to download. In the end I just downloaded the tar files and installed them into app/plugins and added var $components = array(‘DebugKit.Toolbar’); to app_controller.php.

That’s all there was to it

Follow

Get every new post delivered to your Inbox.