mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
Merge branch 'master' of https://github.com/top-think/framework
This commit is contained in:
17
helper.php
17
helper.php
@@ -354,22 +354,25 @@ if (!function_exists('cache')) {
|
||||
{
|
||||
if (is_array($options)) {
|
||||
// 缓存操作的同时初始化
|
||||
Cache::connect($options);
|
||||
$cache = Cache::connect($options);
|
||||
} elseif (is_array($name)) {
|
||||
// 缓存初始化
|
||||
return Cache::connect($name);
|
||||
} else {
|
||||
$cache = Cache::init();
|
||||
}
|
||||
|
||||
if (is_null($name)) {
|
||||
return Cache::clear($value);
|
||||
return $cache->clear($value);
|
||||
} elseif ('' === $value) {
|
||||
// 获取缓存
|
||||
return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
|
||||
return 0 === strpos($name, '?') ? $cache->has(substr($name, 1)) : $cache->get($name);
|
||||
} elseif (is_null($value)) {
|
||||
// 删除缓存
|
||||
return Cache::rm($name);
|
||||
return $cache->rm($name);
|
||||
} elseif (0 === strpos($name, '?') && '' !== $value) {
|
||||
$expire = is_numeric($options) ? $options : null;
|
||||
return Cache::remember(substr($name, 1), $value, $expire);
|
||||
return $cache->remember(substr($name, 1), $value, $expire);
|
||||
} else {
|
||||
// 缓存数据
|
||||
if (is_array($options)) {
|
||||
@@ -378,9 +381,9 @@ if (!function_exists('cache')) {
|
||||
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
|
||||
}
|
||||
if (is_null($tag)) {
|
||||
return Cache::set($name, $value, $expire);
|
||||
return $cache->set($name, $value, $expire);
|
||||
} else {
|
||||
return Cache::tag($tag)->set($name, $value, $expire);
|
||||
return $cache->tag($tag)->set($name, $value, $expire);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
protected static $links = [];
|
||||
// 数据库配置
|
||||
protected $connection = [];
|
||||
// 父关联模型对象
|
||||
protected $parent;
|
||||
// 数据库查询对象
|
||||
protected $query;
|
||||
// 当前模型名称
|
||||
@@ -219,6 +221,29 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置父关联对象
|
||||
* @access public
|
||||
* @param Model $model 模型对象
|
||||
* @return $this
|
||||
*/
|
||||
public function setParent($model)
|
||||
{
|
||||
$this->parent = $model;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取父关联对象
|
||||
* @access public
|
||||
* @return Model
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据对象值
|
||||
* @access public
|
||||
@@ -283,7 +308,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
// 检测修改器
|
||||
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
||||
if (method_exists($this, $method)) {
|
||||
$value = $this->$method($value, $data);
|
||||
$value = $this->$method($value, array_merge($this->data, $data));
|
||||
} elseif (isset($this->type[$name])) {
|
||||
// 类型转换
|
||||
$value = $this->writeTransform($value, $this->type[$name]);
|
||||
@@ -458,10 +483,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
} elseif ($notFound) {
|
||||
$method = Loader::parseName($name, 1, false);
|
||||
if (method_exists($this, $method) && $this->$method() instanceof Relation) {
|
||||
// 清空之前的查询参数
|
||||
$this->$method()->removeOption();
|
||||
$modelRelation = $this->$method();
|
||||
// 不存在该字段 获取关联数据
|
||||
$value = $this->$method()->getRelation();
|
||||
$value = $this->getRelationData($modelRelation);
|
||||
// 保存关联对象值
|
||||
$this->data[$name] = $value;
|
||||
} else {
|
||||
@@ -471,6 +495,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取关联模型数据
|
||||
* @access public
|
||||
* @param Relation $modelRelation 模型关联对象
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getRelationData($modelRelation)
|
||||
{
|
||||
if ($this->parent && get_class($this->parent) == $modelRelation->getModel()) {
|
||||
$value = $this->parent;
|
||||
} else {
|
||||
// 首先获取关联数据
|
||||
$value = $modelRelation->removeOption()->getRelation();
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据读取 类型转换
|
||||
* @access public
|
||||
@@ -1217,7 +1258,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
/**
|
||||
* 返回模型的错误信息
|
||||
* @access public
|
||||
* @return string
|
||||
* @return string|array
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
@@ -1812,16 +1853,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
public static function __callStatic($method, $params)
|
||||
public static function __callStatic($method, $args)
|
||||
{
|
||||
$model = new static();
|
||||
|
||||
if (isset(static::$db)) {
|
||||
$query = static::$db;
|
||||
static::$db = null;
|
||||
} else {
|
||||
$query = (new static())->db();
|
||||
$query = $model->db();
|
||||
}
|
||||
|
||||
return call_user_func_array([$query, $method], $params);
|
||||
if (method_exists($model, 'scope' . $method)) {
|
||||
// 动态调用命名范围
|
||||
$method = 'scope' . $method;
|
||||
array_unshift($args, $query);
|
||||
|
||||
call_user_func_array([$model, $method], $args);
|
||||
return $query;
|
||||
} else {
|
||||
return call_user_func_array([$query, $method], $args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,7 +50,16 @@ class BelongsTo extends OneToOne
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find();
|
||||
$relationModel = $this->query
|
||||
->where($this->localKey, $this->parent->$foreignKey)
|
||||
->relation($subRelation)
|
||||
->find();
|
||||
|
||||
if ($relationModel) {
|
||||
$relationModel->setParent(clone $this->parent);
|
||||
}
|
||||
|
||||
return $relationModel;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,6 +139,8 @@ class BelongsTo extends OneToOne
|
||||
$relationModel = null;
|
||||
} else {
|
||||
$relationModel = $data[$result->$foreignKey];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
}
|
||||
|
||||
if ($relationModel && !empty($this->bindAttr)) {
|
||||
@@ -161,6 +172,8 @@ class BelongsTo extends OneToOne
|
||||
$relationModel = null;
|
||||
} else {
|
||||
$relationModel = $data[$result->$foreignKey];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
}
|
||||
if ($relationModel && !empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\model\relation;
|
||||
|
||||
use think\Db;
|
||||
use think\db\Query;
|
||||
use think\Loader;
|
||||
use think\Model;
|
||||
@@ -47,7 +46,14 @@ class HasMany extends Relation
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
return $this->relation($subRelation)->select();
|
||||
$list = $this->relation($subRelation)->select();
|
||||
$parent = clone $this->parent;
|
||||
|
||||
foreach ($list as &$model) {
|
||||
$model->setParent($parent);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,6 +90,11 @@ class HasMany extends Relation
|
||||
if (!isset($data[$result->$localKey])) {
|
||||
$data[$result->$localKey] = [];
|
||||
}
|
||||
|
||||
foreach ($data[$result->$localKey] as &$relationModel) {
|
||||
$relationModel->setParent(clone $result);
|
||||
}
|
||||
|
||||
$result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey]));
|
||||
}
|
||||
}
|
||||
@@ -108,6 +119,11 @@ class HasMany extends Relation
|
||||
if (!isset($data[$result->$localKey])) {
|
||||
$data[$result->$localKey] = [];
|
||||
}
|
||||
|
||||
foreach ($data[$result->$localKey] as &$relationModel) {
|
||||
$relationModel->setParent(clone $result);
|
||||
}
|
||||
|
||||
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\model\relation;
|
||||
|
||||
use think\Db;
|
||||
use think\db\Query;
|
||||
use think\Exception;
|
||||
use think\Loader;
|
||||
@@ -57,6 +56,7 @@ class HasManyThrough extends Relation
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
|
||||
return $this->relation($subRelation)->select();
|
||||
}
|
||||
|
||||
@@ -96,8 +96,7 @@ class HasManyThrough extends Relation
|
||||
* @return void
|
||||
*/
|
||||
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
/**
|
||||
* 预载入关联查询 返回模型对象
|
||||
@@ -110,8 +109,7 @@ class HasManyThrough extends Relation
|
||||
* @return void
|
||||
*/
|
||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
/**
|
||||
* 关联统计
|
||||
@@ -121,8 +119,7 @@ class HasManyThrough extends Relation
|
||||
* @return integer
|
||||
*/
|
||||
public function relationCount($result, $closure)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
/**
|
||||
* 执行基础查询(进执行一次)
|
||||
|
||||
@@ -50,7 +50,13 @@ class HasOne extends OneToOne
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
// 判断关联类型执行查询
|
||||
return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
|
||||
$relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
|
||||
|
||||
if ($relationModel) {
|
||||
$relationModel->setParent(clone $this->parent);
|
||||
}
|
||||
|
||||
return $relationModel;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,10 +138,12 @@ class HasOne extends OneToOne
|
||||
$relationModel = null;
|
||||
} else {
|
||||
$relationModel = $data[$result->$localKey];
|
||||
}
|
||||
if ($relationModel && !empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
}
|
||||
// 设置关联属性
|
||||
$result->setAttr($attr, $relationModel);
|
||||
@@ -163,12 +171,14 @@ class HasOne extends OneToOne
|
||||
$relationModel = null;
|
||||
} else {
|
||||
$relationModel = $data[$result->$localKey];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
}
|
||||
|
||||
if ($relationModel && !empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
$result->setAttr(Loader::parseName($relation), $relationModel);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\model\relation;
|
||||
|
||||
use think\Db;
|
||||
use think\db\Query;
|
||||
use think\Exception;
|
||||
use think\Loader;
|
||||
@@ -56,7 +55,14 @@ class MorphMany extends Relation
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
return $this->relation($subRelation)->select();
|
||||
$list = $this->relation($subRelation)->select();
|
||||
$parent = clone $this->parent;
|
||||
|
||||
foreach ($list as &$model) {
|
||||
$model->setParent($parent);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,6 +125,10 @@ class MorphMany extends Relation
|
||||
if (!isset($data[$result->$pk])) {
|
||||
$data[$result->$pk] = [];
|
||||
}
|
||||
foreach ($data[$result->$pk] as &$relationModel) {
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
}
|
||||
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk]));
|
||||
}
|
||||
}
|
||||
@@ -141,6 +151,12 @@ class MorphMany extends Relation
|
||||
$this->morphKey => $result->$pk,
|
||||
$this->morphType => $this->type,
|
||||
], $relation, $subRelation, $closure);
|
||||
|
||||
foreach ($data[$result->$pk] as &$relationModel) {
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
}
|
||||
|
||||
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\model\relation;
|
||||
|
||||
use think\Db;
|
||||
use think\db\Query;
|
||||
use think\Exception;
|
||||
use think\Loader;
|
||||
@@ -56,7 +55,13 @@ class MorphOne extends Relation
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $this->query]);
|
||||
}
|
||||
return $this->relation($subRelation)->find();
|
||||
$relationModel = $this->relation($subRelation)->find();
|
||||
|
||||
if ($relationModel) {
|
||||
$relationModel->setParent(clone $this->parent);
|
||||
}
|
||||
|
||||
return $relationModel;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,9 +122,14 @@ class MorphOne extends Relation
|
||||
// 关联数据封装
|
||||
foreach ($resultSet as $result) {
|
||||
if (!isset($data[$result->$pk])) {
|
||||
$data[$result->$pk] = [];
|
||||
$relationModel = null;
|
||||
} else {
|
||||
$relationModel = $data[$result->$pk];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
}
|
||||
$result->setAttr($attr, $this->resultSetBuild($data[$result->$pk]));
|
||||
|
||||
$result->setAttr($attr, $relationModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,11 +147,21 @@ class MorphOne extends Relation
|
||||
{
|
||||
$pk = $result->getPk();
|
||||
if (isset($result->$pk)) {
|
||||
$pk = $result->$pk;
|
||||
$data = $this->eagerlyMorphToOne([
|
||||
$this->morphKey => $result->$pk,
|
||||
$this->morphKey => $pk,
|
||||
$this->morphType => $this->type,
|
||||
], $relation, $subRelation, $closure);
|
||||
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk]));
|
||||
|
||||
if (isset($data[$pk])) {
|
||||
$relationModel = $data[$pk];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
} else {
|
||||
$relationModel = null;
|
||||
}
|
||||
|
||||
$result->setAttr(Loader::parseName($relation), $relationModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,13 @@ class MorphTo extends Relation
|
||||
// 多态模型
|
||||
$model = $this->parseModel($this->parent->$morphType);
|
||||
// 主键数据
|
||||
$pk = $this->parent->$morphKey;
|
||||
return (new $model)->relation($subRelation)->find($pk);
|
||||
$pk = $this->parent->$morphKey;
|
||||
$relationModel = (new $model)->relation($subRelation)->find($pk);
|
||||
|
||||
if ($relationModel) {
|
||||
$relationModel->setParent(clone $this->parent);
|
||||
}
|
||||
return $relationModel;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,7 +173,11 @@ class MorphTo extends Relation
|
||||
if (!isset($data[$result->$morphKey])) {
|
||||
throw new Exception('relation data not exists :' . $this->model);
|
||||
} else {
|
||||
$result->setAttr($attr, $data[$result->$morphKey]);
|
||||
$relationModel = $data[$result->$morphKey];
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
|
||||
$result->setAttr($attr, $relationModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -220,6 +229,7 @@ class MorphTo extends Relation
|
||||
$pk = $this->parent->{$this->morphKey};
|
||||
$data = (new $model)->with($subRelation)->find($pk);
|
||||
if ($data) {
|
||||
$data->setParent(clone $result);
|
||||
$data->isUpdate(true);
|
||||
}
|
||||
$result->setAttr(Loader::parseName($relation), $data ?: null);
|
||||
|
||||
@@ -245,13 +245,19 @@ abstract class OneToOne extends Relation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($list[$relation])) {
|
||||
$relationModel = new $model($list[$relation]);
|
||||
$relationModel->setParent(clone $result);
|
||||
$relationModel->isUpdate(true);
|
||||
|
||||
if (!empty($this->bindAttr)) {
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
} else {
|
||||
$relationModel = null;
|
||||
}
|
||||
$result->setAttr(Loader::parseName($relation), !isset($relationModel) ? null : $relationModel->isUpdate(true));
|
||||
$result->setAttr(Loader::parseName($relation), $relationModel);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,7 +17,6 @@ namespace tests\thinkphp\library\think;
|
||||
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\Route;
|
||||
|
||||
class requestTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user