Cake Notes

Observations from a self-confessed CakePHP newb

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I’ve been using CakePHP 1.2’s Form Helper on a trial project, and I’ve generally been very pleased with how easy it makes the whole process. I don’t have to worry much about what kind of data I’m collecting or deal with the mechanics of saving; it just works.

Well, mostly. I found some helpful examples of Form Helper usage, but kept getting stuck on one field, in my case called “expiration”, which was a date field in MySQL.

The Form Helper did a great job of automatically outputting some select boxes to let users put in the date, but the date was not saved. A Google search was surprisingly unhelpful, but fortunately, I did eventually figure out how to do it from two sources.

The first was from the CakePHP 1.1 manual (but it’s still relevant in this case); search for “Concatenating time data before saving a model”. That’s basically the method I’ve gone with at this point:

$this->data['Job']['expiration'] = date('Y-m-d', mktime(
0,
0,
null,
intval($this->data['Job']['expiration_month']),
intval($this->data['Job']['expiration_day']),
intval($this->data['Job']['expiration_year'])));

if ($this->Job->save($this->data))
{
$this->Session->setFlash('Your job has been saved.');
$this->redirect('/users/index');
}

The second was a post on cakebaker about beforeSave(). This looks like a more elegant way of doing it, but I’m not sure if it requires a core change (it did when he wrote it but it’s been a while). In this case, what I have now works, so I’m not going to mess with it, but I’m particularly posting this so that I remember this method in the future.

sarah   CakePHP 1.1, CakePHP 1.2, Form, Helpers December 2007