/**
* Delete a record. If the model is loaded, delete the current id.
* If not loaded, load model through the $id parameter and delete.
*/
public function delete($id = null)
{
if ($this->loaded() && !is_null($id) && $id !== $this->id) {
throw $this->exception('Unable to determine which record to delete');
}
if (!is_null($id) && (!$this->loaded() || $this->loaded() && $id !== $this->id)) {
$this->load($id);
}
if (!$this->loaded()) {
throw $this->exception('Unable to determine which record to delete');
}
$id = $this->id;
$this->hook('beforeDelete', array($id));
$this->controller->delete($this, $id);
$this->hook('afterDelete', array($id));
$this->unload();
return $this;
}