我已经接受了上面@sascha 的答案,因为我喜欢模型中的“外部”映射器的想法,但是由于各种原因,提供的代码不起作用。根据他的建议,这是我最后使用的:
class Settings {
private $mapper;
function exists($key){
$this->mapper->load(array('`key` = ?', $key));
return !$this->mapper->dry();
}
function get($key) {
$this->mapper->load(array('`key` = ?', $key));
return $this->mapper->get('value');
}
function set($key, $value = '') {
$this->mapper->load(array('`key` = ?', $key));
$this->mapper->set('key',$key);
$this->mapper->set('value',$value);
$this->mapper->save();
}
function __construct() {
$this->mapper = new \DB\SQL\Mapper(\Base::instance()->get('DB'),'settings');
}
}