/**
  * Deletes the current record if there is a token and identifier
  * by setting active = 0
  * and clears memcache
  *
  * Triggers events: before_delete and after_delete
  * Refreshes $this->_belongs_to
  *
  * @return array   the response array (after_fail or after_save)
  */
 public function delete()
 {
     if ($this->_in_transaction) {
         $this->addInternalError('fatal_error', array('message' => 'Cannot delete current instance while in transaction.', 'type' => 'invalid_action'));
         return;
     }
     $id = $this->getID();
     if ($this->_use_token_validation && ($this->_token != Model::generateToken($id, $this->_primary_table) || !$this->_token)) {
         $this->addInternalError('invalid_token');
     }
     if (!$id) {
         $this->addInternalError('identifier_not_set_for_delete');
     }
     if ($this->_errors) {
         return $this->errorResponse();
     }
     $this->_in_transaction = true;
     $this->startTransaction();
     $now = aql::now();
     $fields = array('active' => 0, 'mod_time' => $now, 'update_time' => $now);
     if (defined('PERSON_ID') && PERSON_ID) {
         $fields['mod__person_id'] = $fields['update__person_id'] = PERSON_ID;
     }
     # load the object
     # so that we have the information left over for after_delete hooks
     $this->loadDB($id);
     if ($this->methodExists('beforeDelete')) {
         $this->tryCallable(array($this, 'beforeDelete'));
     }
     if ($this->hasFailedTransaction() || $this->_errors) {
         return $this->getTransactionResponse(true);
     }
     if (aql::update($this->_primary_table, $fields, $id)) {
         # clears the memcache of stored objects of this identifier.
         $delete_key = function ($m) use($id) {
             $tmp = new $m();
             $key = $tmp->getMemKey($id);
             \Sky\Memcache::delete($key);
         };
         $delete_key($this->_model_name);
         foreach ($this->getModelDependencies() as $m) {
             $delete_key($m);
         }
         if ($this->methodExists('afterDelete')) {
             $this->tryCallable(array($this, 'afterDelete'));
         }
         if ($this->hasFailedTransaction() || $this->_errors) {
             return $this->getTransactionResponse(true);
         }
         $this->refreshBelongsTo();
         $re = $this->getTransactionResponse();
         unset($re['data'], $re['_token']);
         return $re;
     } else {
         $this->addInternalError('model_save_failure', array('message' => 'Error deleting record.'));
         return $this->getTransactionResponse(true);
     }
 }