/**
* Saves record with current controller. Uses $this->id as primary key.
* If not set, new record is created.
*/
public function save()
{
$this->hook('beforeSave', array($this->id));
$is_update = $this->loaded();
if ($is_update) {
$source = array();
foreach ($this->dirty as $name => $junk) {
$source[$name] = $this->get($name);
}
// No save needed, nothing was changed
if (empty($source)) {
return $this;
}
$this->hook('beforeUpdate', array(&$source));
} else {
// Collect all data of a new record
$source = $this->get();
$this->hook('beforeInsert', array(&$source));
}
if ($this->controller) {
$this->id = $this->controller->save($this, $this->id, $source);
}
if ($is_update) {
$this->hook('afterUpdate');
} else {
$this->hook('afterInsert');
}
if ($this->loaded()) {
$this->dirty = array();
$this->hook('afterSave', array($this->id));
}
return $this;
}