mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进一堆多关联
This commit is contained in:
@@ -764,17 +764,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
$class = new static();
|
$class = new static();
|
||||||
$joinName = strtolower(basename(str_replace('\\', '/', get_called_class())));
|
|
||||||
$joinTable = Db::name($joinName)->getTableName();
|
|
||||||
$db->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
|
||||||
foreach ($with as $key => $name) {
|
foreach ($with as $key => $name) {
|
||||||
|
$i = 0;
|
||||||
$model = $class->$name();
|
$model = $class->$name();
|
||||||
list($type, $foreignKey, $localKey) = $class->relation;
|
list($type, $foreignKey, $localKey) = $class->relation;
|
||||||
if (in_array($type, [self::HAS_ONE, self::BELONGS_TO])) {
|
if (in_array($type, [self::HAS_ONE, self::BELONGS_TO])) {
|
||||||
|
if (0 == $i) {
|
||||||
|
$joinName = strtolower(basename(str_replace('\\', '/', get_called_class())));
|
||||||
|
$joinTable = Db::name($joinName)->getTableName();
|
||||||
|
$db->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
||||||
|
}
|
||||||
|
|
||||||
// 预载入封装
|
// 预载入封装
|
||||||
$table = $model::getTableName();
|
$table = $model::getTableName();
|
||||||
$name = strtolower(basename(str_replace('\\', '/', $model)));
|
$name = strtolower(basename(str_replace('\\', '/', $model)));
|
||||||
$db->join($table . ' ' . $name, $joinName . '.' . $localKey . '=' . $name . '.' . $foreignKey)->field(true, false, $table, $name);
|
$db->join($table . ' ' . $name, $joinName . '.' . $localKey . '=' . $name . '.' . $foreignKey)->field(true, false, $table, $name);
|
||||||
|
$i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $db->with($with)->model(get_called_class());
|
return $db->with($with)->model(get_called_class());
|
||||||
@@ -827,6 +833,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
foreach ($resultSet as &$result) {
|
foreach ($resultSet as &$result) {
|
||||||
if (isset($data[$result->$localKey])) {
|
if (isset($data[$result->$localKey])) {
|
||||||
$result->__set($relation, $data[$result->$localKey]);
|
$result->__set($relation, $data[$result->$localKey]);
|
||||||
|
} else {
|
||||||
|
$result->__set($relation, []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -872,9 +880,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$data = $this->modelRelationQuery($model, $resultSet, [$foreignKey => $result->$localKey], $relation, $subRelation);
|
$data = $this->modelRelationQuery($model, $resultSet, [$foreignKey => $result->$localKey], $relation, $subRelation);
|
||||||
|
|
||||||
// 关联数据封装
|
// 关联数据封装
|
||||||
if (isset($data[$result->$localKey])) {
|
if (!isset($data[$result->$localKey])) {
|
||||||
$result->__set($relation, $data[$result->$localKey]);
|
$data[$result->$localKey] = [];
|
||||||
}
|
}
|
||||||
|
$result->__set($relation, $data[$result->$localKey]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -915,10 +924,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($list[$modelName])) {
|
if (!isset($list[$modelName])) {
|
||||||
// 设置关联模型属性
|
// 设置关联模型属性
|
||||||
$result->__set($relation, new $model($list[$modelName]));
|
$list[$modelName] = [];
|
||||||
}
|
}
|
||||||
|
$result->__set($relation, new $model($list[$modelName]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -938,6 +948,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$list = $model::where($where)->with($subRelation)->select();
|
$list = $model::where($where)->with($subRelation)->select();
|
||||||
|
|
||||||
// 组装模型数据
|
// 组装模型数据
|
||||||
|
$data = [];
|
||||||
foreach ($list as $set) {
|
foreach ($list as $set) {
|
||||||
$data[$set->$foreignKey][] = $set;
|
$data[$set->$foreignKey][] = $set;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user