- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明 Made-with: Cursor
79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ScottPan Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace crmeb\topthink;
|
|
|
|
use Closure;
|
|
|
|
/**
|
|
* 远程一对多关联
|
|
* Class HasManyThrough
|
|
* @author 等风来
|
|
* @email 136327134@qq.com
|
|
* @date 2023/10/11
|
|
* @package crmeb\topthink
|
|
*/
|
|
class HasManyThrough extends \think\model\relation\HasManyThrough
|
|
{
|
|
|
|
/**
|
|
* @param array $where
|
|
* @param string $key
|
|
* @param array $subRelation
|
|
* @param Closure|null $closure
|
|
* @param array $cache
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @author 等风来
|
|
* @email 136327134@qq.com
|
|
* @date 2023/10/11
|
|
*/
|
|
protected function eagerlyWhere(array $where, string $key, array $subRelation = [], Closure $closure = null, array $cache = []): array
|
|
{
|
|
// 预载入关联查询 支持嵌套预载入
|
|
$throughList = $this->through->where($where)->select();
|
|
$keys = $throughList->column($this->throughPk, $this->throughPk);
|
|
|
|
if ($closure) {
|
|
$this->baseQuery = true;
|
|
$closure($this->query);
|
|
}
|
|
|
|
$throughKey = $this->throughKey;
|
|
|
|
if ($this->baseQuery) {
|
|
$throughKey = $this->query->getTable() . '.' . $this->throughKey;
|
|
}
|
|
|
|
$withLimit = $this->query->getOptions('limit');
|
|
if ($withLimit) {
|
|
$this->query->removeOption('limit');
|
|
}
|
|
|
|
$list = $this->query
|
|
->where($throughKey, 'in', $keys)
|
|
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
|
|
->select();
|
|
|
|
// 组装模型数据
|
|
$data = [];
|
|
$keys = $throughList->column($this->foreignKey, $this->throughPk);
|
|
|
|
foreach ($list as $set) {
|
|
$key = $keys[$set->{$this->throughKey}];
|
|
|
|
if ($withLimit && isset($data[$key]) && count($data[$key]) >= $withLimit) {
|
|
continue;
|
|
}
|
|
|
|
$data[$key][] = $set;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|