Simple Settings Model,, for mysql db conector
<?php
/*
$settings = get::model('settings'); /// settings are controler spific Unless set as argument
$settings->hello(10); /// Sets VALUE, Returns Value
$settings->hello() /// Returns Value
*/
class settingsModel extends model {
protected $_table = 'settings';
protected $_area;
public function __construct($area = null) {
if($area != null)
$this->_area = $area;
else
$this->_area = mvc::$controller;
}
public function __call($name, array $value = null) {
if($value == null){
return $this->_get($name);
}else{
return $this->_set($name,$value[0]);
}
}
protected function _set($key,$value){
$data = array('setting_area'=>$this->_area,'setting_key'=>$key,'setting_value'=>$value);
$args = array('table' => $this->_table,
'vals' => $this->db->cleanString($data));
$sql = $this->db->insertOrUpdateSql($args);
$ret = $this->db->query($sql);
if(!$ret){
$this->_init();
return $this->_set($key,$value);
}else
return true;
}
protected function _get($key){
$return = 'false';
$sql = 'select * from '.$this->_table.' where setting_area='
. $this->db->cleanString($this->_area)
. ' and setting_key=' . $this->db->cleanString($key);
$res = $this->db->query($sql);
if($res){
$row = $res->fetch_assoc();
if($row) $return = $row['setting_value'];
}else{
$this->_init();
$return = $this->_get($key);
}
return $return;
}
protected function _init(){
$cols = 'setting_area varchar(255) not null,
setting_key varchar(255) not null,
setting_value varchar(255) not null,';
$sql = 'create table '.$this->_table.' (' . $cols . ' primary key(setting_area, setting_key))';
$this->db->query($sql);
}
}
Posted May. 21, 2012 by Bradley Matusiak