我有一个设置表,可以存储各种租户设置:
CREATE TABLE `client_settings` (
`key` varchar(255) DEFAULT NULL,
`value` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
是否有一种简单的方法可以将映射器用于以下内容:
$mapper->get('key')
或
$mapper->set('key','value')
答案 0 :(得分:0)
映射器中没有内置这样的东西。您必须为映射器创建映射器,这样做。我做了一个非常基本的例子:
class client_settings {
private $mapper;
function __construct() {
$f3 = Base::instance();
$mapper = new \sql\mapper($f3->get('DB'), 'client_settings');
}
function get($key) {
$this->mapper->load('key = ?', $key);
return $this->mapper->key;
}
function set($key, $value = '') {
$this->mapper->load('key = ?', $key);
$this->mapper->key = $value;
$this->mapper->save();
}
}
答案 1 :(得分:0)
我接受了@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');
}
}