改进HAS ONE BELONGS_TO 关联预载入查询

This commit is contained in:
thinkphp
2016-04-12 15:24:09 +08:00
parent 880562b42c
commit c5cf46c392
2 changed files with 72 additions and 39 deletions

View File

@@ -153,10 +153,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
throw new Exception('data type invalid', 10300); throw new Exception('data type invalid', 10300);
} }
$this->data = $data; $this->data = $data;
// 标记字段为更改
foreach ($data as $key => $val) {
$this->change[] = $key;
}
return $this; return $this;
} }
@@ -740,6 +736,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this; return $this;
} }
/**
* 使用关联预载入查询
* @access public
* @param string|array $relations 关联名
* @return Db
*/
public static function with($with = '') public static function with($with = '')
{ {
if (is_string($with)) { if (is_string($with)) {
@@ -750,7 +752,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $db; return $db;
} }
$class = new static(); $class = new static();
$joinName = basename(str_replace('\\', '/', static::class)); $joinName = strtolower(basename(str_replace('\\', '/', static::class)));
$joinTable = Db::name($joinName)->getTableName(); $joinTable = Db::name($joinName)->getTableName();
$db->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName); $db->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
foreach ($with as $name) { foreach ($with as $name) {
@@ -759,14 +761,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (in_array($type, [self::HAS_ONE, self::BELONGS_TO])) { if (in_array($type, [self::HAS_ONE, self::BELONGS_TO])) {
// 预载入封装 // 预载入封装
$table = $model::getTableName(); $table = $model::getTableName();
$name = basename(str_replace('\\', '/', $model)); $name = strtolower(basename(str_replace('\\', '/', $model)));
$db->field($joinName . '.' . $localKey . ' as ' . $localKey) $db->join($table . ' ' . $name, $joinName . '.' . $localKey . '=' . $name . '.' . $foreignKey)->field(true, false, $table, $name);
->join($table . ' ' . $name, $joinName . '.' . $localKey . '=' . $name . '.' . $foreignKey)
->field(true, false, $table, $name);
} }
} }
return $db->with($with); return $db->with($with)->model(static::class);
} }
/** /**
@@ -778,49 +777,77 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function eagerly($resultSet, $relation) public function eagerly($resultSet, $relation)
{ {
$relations = is_string($relation) ? explode(',', $relation) : $relation; $relations = is_string($relation) ? explode(',', $relation) : $relation;
foreach ($relations as $relation) { foreach ($relations as $relation) {
$range = [];
$data = [];
if (strpos($relation, '.')) { if (strpos($relation, '.')) {
list($relation, $subRelation) = explode('.', $relation); list($relation, $subRelation) = explode('.', $relation);
} }
$model = $this->$relation(); // 执行关联方法
$model = $this->$relation();
// 获取关联信息
list($type, $foreignKey, $localKey) = $this->relation; list($type, $foreignKey, $localKey) = $this->relation;
foreach ($resultSet as $result) {
// 获取关联外键列表
$range[] = $result->$localKey;
}
// 预载入关联查询
if (isset($subRelation)) {
// 嵌套预载入
$list = $model::where($foreignKey, 'in', $range)->with($subRelation)->select();
} else {
$list = $model::where($foreignKey, 'in', $range)->select();
}
switch ($type) { switch ($type) {
case self::HAS_ONE: case self::HAS_ONE:
case self::BELONGS_TO: case self::BELONGS_TO:
/* $list = [];
foreach ($list as $set) { $modelName = strtolower(basename(str_replace('\\', '/', $model)));
$data[$set->$foreignKey] = $set; $currName = strtolower($this->name);
}*/ foreach ($resultSet as &$result) {
// 重新组装模型数据
foreach ($result->toarray() as $key => $val) {
if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key);
$list[$name][$attr] = $val;
unset($result->$key);
}
}
// 当前模型属性设置
if (isset($list[$currName])) {
foreach ($list[$currName] as $name => $val) {
$result->__set($name, $val);
}
}
if (isset($list[$modelName])) {
// 设置关联模型属性
$result->__set($relation, new $model($list[$modelName]));
}
}
break; break;
case self::HAS_MANY: case self::HAS_MANY:
case self::BELONGS_TO_MANY: case self::BELONGS_TO_MANY:
$range = [];
$data = [];
foreach ($resultSet as $result) {
// 获取关联外键列表
$range[] = $result->$localKey;
}
// 预载入关联查询
if (isset($subRelation)) {
// 嵌套预载入
$list = $model::where($foreignKey, 'in', $range)->with($subRelation)->select();
} else {
$list = $model::where($foreignKey, 'in', $range)->select();
}
// 组装模型数据
foreach ($list as $set) { foreach ($list as $set) {
$data[$set->$foreignKey][] = $set; $data[$set->$foreignKey][] = $set;
} }
// 关联数据封装
foreach ($resultSet as &$result) {
if (isset($data[$result->$localKey])) {
$result->__set($relation, $data[$result->$localKey]);
}
}
break; break;
} }
// 关联数据封装
foreach ($resultSet as &$result) {
if (isset($data[$result->$localKey])) {
$result->$relation = $data[$result->$localKey];
}
}
$this->relation = []; $this->relation = [];
} }
return $resultSet; return $resultSet;

View File

@@ -2057,19 +2057,25 @@ abstract class Driver
protected function parsePkWhere($data) protected function parsePkWhere($data)
{ {
$pk = $this->getTableInfo('', 'pk'); $pk = $this->getTableInfo('', 'pk');
// 获取当前数据表
if (!empty($this->options['alias'])) {
$alias = $this->options['alias'];
}
if (is_string($pk)) { if (is_string($pk)) {
$key = isset($alias) ? $alias . '.' . $pk : $pk;
// 根据主键查询 // 根据主键查询
if (is_array($data)) { if (is_array($data)) {
$where[$pk] = isset($data[$pk]) ? $data[$pk] : ['in', $data]; $where[$key] = isset($data[$pk]) ? $data[$pk] : ['in', $data];
} else { } else {
$where[$pk] = strpos($data, ',') ? ['IN', $data] : $data; $where[$key] = strpos($data, ',') ? ['IN', $data] : $data;
} }
$this->options['where']['AND'] = $where; $this->options['where']['AND'] = $where;
} elseif (is_array($pk) && is_array($data) && !empty($data)) { } elseif (is_array($pk) && is_array($data) && !empty($data)) {
// 根据复合主键查询 // 根据复合主键查询
foreach ($pk as $key) { foreach ($pk as $key) {
if (isset($data[$key])) { if (isset($data[$key])) {
$where[$key] = $data[$key]; $attr = isset($alias) ? $alias . '.' . $key : $key;
$where[$attr] = $data[$key];
} else { } else {
throw new Exception('miss complex primary data'); throw new Exception('miss complex primary data');
} }