0

我有一个设置表,其中存储各种租户设置,如下所示:

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')
4

2 回答 2

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();
    }
}
于 2014-06-02T08:13:46.310 回答
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');
    }

}
于 2014-06-09T00:10:17.207 回答