I added an update to CakePHP Book: beforeSave()
Also, if you add a primary key which would turn an “insert” into an “update” within beforeSave() you’ll need to set $this->__exists = true;… the call to $this->exists(); happens in model.php before the callback to beforeSave().
function beforeSave() { if (!isset($this->data[$this->name]['id']) && isset($this->data[$this->name]['unique_field'])) { $found = $this->find("first",array( "recursive" => -1, "fields" => array("id"), "conditions" => array("unique_field" => $this->data[$this->name]['unique_field']))); if (!empty($found) && isset($found[$this->name]['id'])) { $this->id = $this->data[$this->name]['id'] = $found[$this->name]['id']; $this->__exists = true; } } return parent::beforeSave(); } That’s been confusing me a bit recently – setting the ID of a row within beforesave() should change the save to an update, but it was trying to insert with a specified ID and thus, failing… glad to have a simple solution that makes sense… hope that helps someone else…
I’ve been working with CakePHP for a few years now and am very happy with it. I’ve been working with the 1.2 version for a few months (since it went stable) and playing with the Auth and ACL core components.
I’ve decided that ACL is too complicated for most setups, and Auth is fine, but not perfect. So after some research, I switched to the DarkAuth component, which was much better suited to my needs.
I reported this as a ticket, but as it turns out, it’s a design choice.. but it’s enough of a gotcha that I thought I should report it.
What happened The $this->ModelName->create() set the defaults for the next save, which is what I would expect. But on my database there’s a default value set for the ‘created’ field (the standard for datetime columns in PHPmyAdmin) as well as the ‘modified’ and ‘updated’ fields.
We had the very good fortune of attending the first CakePHP workshop, in Raleigh, NC this weekend. Four of the core developers of the project where there, including the project manger and the lead developer; along with one of the guys behind jquery-UI. The non-presenters ranged from people who had never used We had the very good fortune of attending the first CakePHP workshop, in Raleigh, NC this weekend. Four of the core developers of the project where there, including the project manger and the lead developer; along with one of the guys behind jquery-UI.
So here I am, in Raleigh, NC – waiting for the CakePHP Workshop to start… We survived the rain so far, waiting to see if there is any flooding or whatnot.
Have met a few people here so far, all seem very friendly. Looking forward to seeing what happens and what we learn along the way.
I am wearing my “I [heart] LAMP” geek-shirt today, as it seemed appropriate (if a little over the top nerdy).