Php

1 minute read Published:

CakePHP beforeSave() gotcha: need to set $this->__exists to true if setting a primary key

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…

1 minute read Published:

MySQL Master/Slave Replication Monitoring PHP Script

I’m a bit proud of myself on this one… I setup a simple, but clean and configurable, MySQL Master/Slave Replication Monitoring PHP Script: http://code.google.com/p/php-mysql-master-slave-replication-monitor/ (docs) It’s Open Source and free, I welcome comments, suggestions, and questions. I think this will be quite useful for anyone who is setting up a pair of MySQL servers as master/slave and want to be sure their replication works as it should.