mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进Model类
This commit is contained in:
@@ -793,6 +793,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$relations = is_string($relation) ? explode(',', $relation) : $relation;
|
$relations = is_string($relation) ? explode(',', $relation) : $relation;
|
||||||
|
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
|
$subRelation = '';
|
||||||
if (strpos($relation, '.')) {
|
if (strpos($relation, '.')) {
|
||||||
list($relation, $subRelation) = explode('.', $relation);
|
list($relation, $subRelation) = explode('.', $relation);
|
||||||
}
|
}
|
||||||
@@ -804,32 +805,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
switch ($type) {
|
switch ($type) {
|
||||||
case self::HAS_ONE:
|
case self::HAS_ONE:
|
||||||
case self::BELONGS_TO:
|
case self::BELONGS_TO:
|
||||||
$list = [];
|
|
||||||
$modelName = strtolower(basename(str_replace('\\', '/', $model)));
|
|
||||||
$currName = strtolower($this->name);
|
|
||||||
foreach ($resultSet as &$result) {
|
foreach ($resultSet as &$result) {
|
||||||
// 重新组装模型数据
|
// 模型关联组装
|
||||||
foreach ($result->toarray() as $key => $val) {
|
$this->modelRelationBuild($model, $relation, $result);
|
||||||
if (strpos($key, '__')) {
|
|
||||||
list($name, $attr) = explode('__', $key);
|
|
||||||
if (in_array($name, [$currName, $modelName])) {
|
|
||||||
$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:
|
||||||
@@ -839,20 +817,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
// 获取关联外键列表
|
// 获取关联外键列表
|
||||||
$range[] = $result->$localKey;
|
$range[] = $result->$localKey;
|
||||||
}
|
}
|
||||||
|
$where[$foreignKey] = ['in', $range];
|
||||||
// 预载入关联查询
|
$data = $this->modelRelationQuery($model, $where, $relation, $subRelation);
|
||||||
if (isset($subRelation)) {
|
|
||||||
// 嵌套预载入
|
|
||||||
$list = $model::where($foreignKey, 'in', $range)->with($subRelation)->select();
|
|
||||||
} else {
|
|
||||||
$list = $model::where($foreignKey, 'in', $range)->select();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 组装模型数据
|
|
||||||
$data = [];
|
|
||||||
foreach ($list as $set) {
|
|
||||||
$data[$set->$foreignKey][] = $set;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关联数据封装
|
// 关联数据封装
|
||||||
foreach ($resultSet as &$result) {
|
foreach ($resultSet as &$result) {
|
||||||
@@ -881,6 +847,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$relations = is_string($relation) ? explode(',', $relation) : $relation;
|
$relations = is_string($relation) ? explode(',', $relation) : $relation;
|
||||||
|
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
|
$subRelation = '';
|
||||||
if (strpos($relation, '.')) {
|
if (strpos($relation, '.')) {
|
||||||
list($relation, $subRelation) = explode('.', $relation);
|
list($relation, $subRelation) = explode('.', $relation);
|
||||||
}
|
}
|
||||||
@@ -892,64 +859,86 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
switch ($type) {
|
switch ($type) {
|
||||||
case self::HAS_ONE:
|
case self::HAS_ONE:
|
||||||
case self::BELONGS_TO:
|
case self::BELONGS_TO:
|
||||||
$list = [];
|
// 模型关联组装
|
||||||
$modelName = strtolower(basename(str_replace('\\', '/', $model)));
|
$this->modelRelationBuild($model, $relation, $result);
|
||||||
$currName = strtolower($this->name);
|
|
||||||
|
|
||||||
// 重新组装模型数据
|
|
||||||
foreach ($result->toarray() as $key => $val) {
|
|
||||||
if (strpos($key, '__')) {
|
|
||||||
list($name, $attr) = explode('__', $key);
|
|
||||||
if (in_array($name, [$currName, $modelName])) {
|
|
||||||
$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:
|
||||||
|
$where[$foreignKey] = $result->$localKey;
|
||||||
// 预载入关联查询
|
$data = $this->modelRelationQuery($model, $resultSet, $where, $relation, $subRelation);
|
||||||
if (isset($subRelation)) {
|
|
||||||
// 嵌套预载入
|
|
||||||
$list = $model::where($foreignKey, $result->$localKey)->with($subRelation)->select();
|
|
||||||
} else {
|
|
||||||
$list = $model::where($foreignKey, $result->$localKey)->select();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 组装模型数据
|
|
||||||
$data = [];
|
|
||||||
foreach ($list as $set) {
|
|
||||||
$data[$set->$foreignKey][] = $set;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关联数据封装
|
// 关联数据封装
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$this->relation = [];
|
$this->relation = [];
|
||||||
}
|
}
|
||||||
$this->eagerly = false;
|
$this->eagerly = false;
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联模型拼装
|
||||||
|
* @access public
|
||||||
|
* @param string $model 模型名称
|
||||||
|
* @param string $relation 关联名
|
||||||
|
* @param Model $result 模型对象实例
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function modelRelationBuild($model, $relation, &$result)
|
||||||
|
{
|
||||||
|
$modelName = strtolower(basename(str_replace('\\', '/', $model)));
|
||||||
|
$currName = strtolower($this->name);
|
||||||
|
// 重新组装模型数据
|
||||||
|
foreach ($result->toarray() as $key => $val) {
|
||||||
|
if (strpos($key, '__')) {
|
||||||
|
list($name, $attr) = explode('__', $key);
|
||||||
|
if (in_array($name, [$currName, $modelName])) {
|
||||||
|
$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]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联模型预查询
|
||||||
|
* @access public
|
||||||
|
* @param string $model 模型名称
|
||||||
|
* @param array $where 关联预查询条件
|
||||||
|
* @param string $relation 关联名
|
||||||
|
* @param string $subRelation 子关联
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function modelRelationQuery($model, $where, $relation, $subRelation = '')
|
||||||
|
{
|
||||||
|
list($type, $foreignKey, $localKey) = $this->relation;
|
||||||
|
|
||||||
|
// 预载入关联查询 支持嵌套预载入
|
||||||
|
$list = $model::where($where)->with($subRelation)->select();
|
||||||
|
|
||||||
|
// 组装模型数据
|
||||||
|
foreach ($list as $set) {
|
||||||
|
$data[$set->$foreignKey][] = $set;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HAS ONE 关联定义
|
* HAS ONE 关联定义
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
Reference in New Issue
Block a user