关联模型架构调整

This commit is contained in:
thinkphp
2016-12-05 08:42:08 +08:00
parent 670b7d13a8
commit d11e4aea10
11 changed files with 1480 additions and 952 deletions

View File

@@ -27,6 +27,8 @@ use think\exception\PDOException;
use think\Loader;
use think\Model;
use think\model\Relation;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;
use think\Paginator;
class Query
@@ -114,6 +116,16 @@ class Query
return $this;
}
/**
* 获取当前的模型对象名
* @access public
* @return Connection
*/
public function getModel()
{
return $this->model;
}
/**
* 指定默认的数据表名(不含前缀)
* @access public
@@ -976,6 +988,20 @@ class Query
return $this;
}
/**
* 去除某个查询参数
* @access public
* @param string $option 参数名
* @return $this
*/
public function removeOption($option)
{
if (isset($this->options[$option])) {
unset($this->options[$option]);
}
return $this;
}
/**
* 指定查询数量
* @access public
@@ -1601,13 +1627,14 @@ class Query
$with = explode(',', $with);
}
$i = 0;
$first = true;
$currentModel = $this->model;
/** @var Model $class */
$class = new $currentModel;
foreach ($with as $key => $relation) {
$closure = false;
$subRelation = '';
$closure = false;
if ($relation instanceof \Closure) {
// 支持闭包查询过滤关联条件
$closure = $relation;
@@ -1620,48 +1647,9 @@ class Query
/** @var Relation $model */
$model = $class->$relation();
$info = $model->getRelationInfo();
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
if (0 == $i) {
$name = Loader::parseName(basename(str_replace('\\', '/', $currentModel)));
$table = $this->getTable();
$alias = isset($info['alias'][$name]) ? $info['alias'][$name] : $name;
$this->table([$table => $alias]);
if (isset($this->options['field'])) {
$field = $this->options['field'];
unset($this->options['field']);
} else {
$field = true;
}
$this->field($field, false, $table, $alias);
}
// 预载入封装
$joinTable = $model->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
$joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $relation;
$this->via($joinAlias);
if (Relation::HAS_ONE == $info['type']) {
$this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['localKey'] . '=' . $joinAlias . '.' . $info['foreignKey'], $info['joinType']);
} else {
$this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['foreignKey'] . '=' . $joinAlias . '.' . $info['localKey'], $info['joinType']);
}
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $this]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
if (!empty($this->options['with_field'])) {
$field = $this->options['with_field'];
unset($this->options['with_field']);
}
} elseif (isset($info['option']['field'])) {
$field = $info['option']['field'];
}
$this->field($field, false, $joinTable, $joinAlias, $relation . '__');
$i++;
if ($model instanceof HasOne || $model instanceof BelongsTo) {
$model->eagerly($this, $relation, $subRelation, $closure, $first);
$first = false;
} elseif ($closure) {
$with[$key] = $closure;
}