数据库配置信息增加query参数用于配置查询对象名称 模型类增加query属性用于配置模型需要的查询对象名称

This commit is contained in:
thinkphp
2016-09-20 12:04:09 +08:00
parent c1a2db30c7
commit 35890dad5c
2 changed files with 11 additions and 5 deletions

View File

@@ -39,11 +39,12 @@ use think\paginator\Collection as PaginatorCollection;
*/
abstract class Model implements \JsonSerializable, \ArrayAccess
{
// 数据库对象池
protected static $links = [];
// 数据库配置
protected $connection = [];
// 数据库查询对象
protected $query;
// 当前模型名称
protected $name;
// 数据表名称
@@ -147,7 +148,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = $this->class;
if (!isset(self::$links[$model])) {
// 设置当前模型 确保查询返回模型对象
$query = Db::connect($this->connection)->model($model);
$query = Db::connect($this->connection)->model($model, $this->query);
// 设置当前数据表和模型名
if (!empty($this->table)) {

View File

@@ -104,6 +104,8 @@ abstract class Connection
'sql_explain' => false,
// Builder类
'builder' => '',
// Query类
'query' => '\\think\\db\\Query',
];
// PDO连接参数
@@ -131,12 +133,14 @@ abstract class Connection
* 创建指定模型的查询对象
* @access public
* @param string $model 模型类名称
* @param string $queryClass 查询对象类名
* @return Query
*/
public function model($model)
public function model($model, $queryClass = '')
{
if (!isset($this->query[$model])) {
$this->query[$model] = new Query($this, $model);
$class = $queryClass ?: $this->config['query'];
$this->query[$model] = new $class($this, $model);
}
return $this->query[$model];
}
@@ -151,7 +155,8 @@ abstract class Connection
public function __call($method, $args)
{
if (!isset($this->query['database'])) {
$this->query['database'] = new Query($this);
$class = $this->config['query'];
$this->query['database'] = new $class($this);
}
return call_user_func_array([$this->query['database'], $method], $args);
}