public function delete($entity = null) {
        if (!is_null($entity)) {
            if (is_string($entity)) {
                $entity = $this->getEntity($entity);
                if (is_null($entity))
                    return false;
            } elseif (is_object($entity)) {
                if (!$entity instanceof Entity)
                    throw new \Exception('Entity must an instance of MidiChloriansPHP\mvc\model\Entity');
            } else
                throw new \Exception('Entity must be a string or an object');

            $deleted = $this->_deleteEntity($entity);
            if ($deleted && $this->isAttached($entity))
                $this->detach($entity);
            return $deleted;
        } else { // delete all entities
            $entityDeletedCount = 0;
            foreach ($this->_entities as &$entity) {
                $deleted = $this->_deleteEntity($entity);
                $entityDeletedCount = $entityDeletedCount + $deleted;
                if ($deleted)
                    $this->detach($entity);
            }

            return $entityDeletedCount;
        }
    }