【问题标题】:Working with key/value schema and Fat Free Framework Mapper使用键/值模式和 Fat Free Framework Mapper
【发布时间】:2014-06-01 22:27:31
【问题描述】:

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

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

【问题讨论】:

    标签: php mysql fat-free-framework


    【解决方案1】:

    映射器中没有内置这样的东西。您必须为映射器创建一个映射器,它可以做到这一点。我做了一个非常基本的例子:

    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();
        }
    }
    

    【讨论】:

    • 我已经接受了答案,我的工作代码贴在下面
    【解决方案2】:

    我接受了上面@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');
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多