$config);
}
if ($config) {
$this->setOptions($config);
}
if (! is_object($this->_db)) {
$this->_db = Fit_Db::instance('default');
}
}
protected function changeAdapter($name, $config = null)
{
$this->_db = Fit_Db::instance($name, $config);
return $this;
}
/**
* @param string $name
* @return Fit_Db_Model
*/
public function setTable($name)
{
$this->_name = (string) $name;
return $this;
}
/**
* @return string
*/
public function getTable()
{
return $this->_name;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->_prefix;
}
public function beginTransaction()
{
$this->_db->beginTransaction();
return true;
}
public function commit()
{
$this->_db->commit();
return true;
}
public function rollBack()
{
$this->_db->rollBack();
return true;
}
/**
* 由字段的KEY-VAL数组构建WHERE条件(AND连接).
* 数组格式如下:
*
* $rows = array('field1' = > '123', 'field2' => 'abc');
*
* 该方法主要是用于兼容原Kohana中Database Query Builder的Where()
*
* @param Array|String $rows
* @return String
*/
public function batchWhere($rows)
{
if (is_array($rows) && count($rows) > 0) {
$where = array();
foreach ($rows as $k => $v) {
$where[] = $this->getAdapter()->quoteIdentifier($k) . ' = \'' . (string) $v . '\'';
}
return implode(' AND ', $where);
} elseif (is_string($rows)) {
return $rows;
}
return '';
}
/**
* 快速获取指定MODEL的实例
*
* @param string $name Model Name.
* @throws Zeed_Exception
* @return Fit_Db_Model
*/
public static function getModel($name)
{
return self::_instance($name);
}
/**
* MODEL单列模式
* 在子类中(注意注释中填写正确的返回值类型, 一般为类名):
*
* public static function instance()
* {
* return parent::_instance(__CLASS__);
* }
*
*
* @param string $model
* @throws Zeed_Exception
* @return Fit_Db_Model
*/
protected static function _instance($model)
{
if (isset(self::$instanceInternalCache[$model]) && is_subclass_of(self::$instanceInternalCache[$model], __CLASS__)) {
return self::$instanceInternalCache[$model];
} elseif (class_exists($model)) {
self::$instanceInternalCache[$model] = new $model();
return self::$instanceInternalCache[$model];
}
throw new Zeed_Exception('Model( ' . $model . ' ) not exists.');
}
}
// End ^ LF ^ UTF-8