1、tp内核更新
2、时间格式bug更新
This commit is contained in:
@@ -17,27 +17,46 @@ use think\Loader;
|
||||
use think\Model;
|
||||
use think\model\Relation;
|
||||
|
||||
/**
|
||||
* Class OneToOne
|
||||
* @package think\model\relation
|
||||
*
|
||||
*/
|
||||
abstract class OneToOne extends Relation
|
||||
{
|
||||
// 预载入方式
|
||||
protected $eagerlyType = 0;
|
||||
// 预载入方式 0 -JOIN 1 -IN
|
||||
protected $eagerlyType = 1;
|
||||
// 当前关联的JOIN类型
|
||||
protected $joinType;
|
||||
// 要绑定的属性
|
||||
protected $bindAttr = [];
|
||||
|
||||
/**
|
||||
* 设置join类型
|
||||
* @access public
|
||||
* @param string $type JOIN类型
|
||||
* @return $this
|
||||
*/
|
||||
public function joinType($type)
|
||||
{
|
||||
$this->joinType = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预载入关联查询(JOIN方式)
|
||||
* @access public
|
||||
* @param Query $query 查询对象
|
||||
* @param string $relation 关联名
|
||||
* @param string $subRelation 子关联
|
||||
* @param \Closure $closure 闭包条件
|
||||
* @param bool $first
|
||||
* @param Query $query 查询对象
|
||||
* @param string $relation 关联名
|
||||
* @param string $subRelation 子关联
|
||||
* @param \Closure $closure 闭包条件
|
||||
* @param bool $first
|
||||
* @return void
|
||||
*/
|
||||
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
|
||||
{
|
||||
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
|
||||
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
|
||||
$alias = $name;
|
||||
if ($first) {
|
||||
$table = $query->getTable();
|
||||
$query->table([$table => $alias]);
|
||||
@@ -52,8 +71,7 @@ abstract class OneToOne extends Relation
|
||||
|
||||
// 预载入封装
|
||||
$joinTable = $this->query->getTable();
|
||||
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
|
||||
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
|
||||
$joinAlias = $relation;
|
||||
$query->via($joinAlias);
|
||||
|
||||
if ($this instanceof BelongsTo) {
|
||||
@@ -64,9 +82,8 @@ abstract class OneToOne extends Relation
|
||||
|
||||
if ($closure) {
|
||||
// 执行闭包查询
|
||||
call_user_func_array($closure, [ & $query]);
|
||||
//指定获取关联的字段
|
||||
//需要在 回调中 调方法 withField 方法,如
|
||||
call_user_func_array($closure, [& $query]);
|
||||
// 使用withField指定获取关联的字段,如
|
||||
// $query->where(['id'=>1])->withField('id,name');
|
||||
if ($query->getOptions('with_field')) {
|
||||
$field = $query->getOptions('with_field');
|
||||
@@ -80,21 +97,40 @@ abstract class OneToOne extends Relation
|
||||
$query->field($field, false, $joinTable, $joinAlias, $relation . '__');
|
||||
}
|
||||
|
||||
/**
|
||||
* 预载入关联查询(数据集)
|
||||
* @param array $resultSet
|
||||
* @param string $relation
|
||||
* @param string $subRelation
|
||||
* @param \Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure);
|
||||
|
||||
/**
|
||||
* 预载入关联查询(数据)
|
||||
* @param Model $result
|
||||
* @param string $relation
|
||||
* @param string $subRelation
|
||||
* @param \Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function eagerlyOne(&$result, $relation, $subRelation, $closure);
|
||||
|
||||
/**
|
||||
* 预载入关联查询(数据集)
|
||||
* @access public
|
||||
* @param array $resultSet 数据集
|
||||
* @param string $relation 当前关联名
|
||||
* @param string $subRelation 子关联名
|
||||
* @param \Closure $closure 闭包
|
||||
* @param string $class 数据集对象名 为空表示数组
|
||||
* @param array $resultSet 数据集
|
||||
* @param string $relation 当前关联名
|
||||
* @param string $subRelation 子关联名
|
||||
* @param \Closure $closure 闭包
|
||||
* @return void
|
||||
*/
|
||||
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
||||
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
|
||||
{
|
||||
if (1 == $this->eagerlyType) {
|
||||
// IN查询
|
||||
$this->eagerlySet($resultSet, $relation, $subRelation, $closure, $class);
|
||||
$this->eagerlySet($resultSet, $relation, $subRelation, $closure);
|
||||
} else {
|
||||
// 模型关联组装
|
||||
foreach ($resultSet as $result) {
|
||||
@@ -106,18 +142,17 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 预载入关联查询(数据)
|
||||
* @access public
|
||||
* @param Model $result 数据对象
|
||||
* @param string $relation 当前关联名
|
||||
* @param string $subRelation 子关联名
|
||||
* @param \Closure $closure 闭包
|
||||
* @param string $class 数据集对象名 为空表示数组
|
||||
* @param Model $result 数据对象
|
||||
* @param string $relation 当前关联名
|
||||
* @param string $subRelation 子关联名
|
||||
* @param \Closure $closure 闭包
|
||||
* @return void
|
||||
*/
|
||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
|
||||
{
|
||||
if (1 == $this->eagerlyType) {
|
||||
// IN查询
|
||||
$this->eagerlyOne($result, $relation, $subRelation, $closure, $class);
|
||||
$this->eagerlyOne($result, $relation, $subRelation, $closure);
|
||||
} else {
|
||||
// 模型关联组装
|
||||
$this->match($this->model, $relation, $result);
|
||||
@@ -127,7 +162,7 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 保存(新增)当前关联数据对象
|
||||
* @access public
|
||||
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
|
||||
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
|
||||
* @return integer
|
||||
*/
|
||||
public function save($data)
|
||||
@@ -144,8 +179,8 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 设置预载入方式
|
||||
* @access public
|
||||
* @param integer $type 预载入方式 0 JOIN查询 1 IN查询
|
||||
* @return this
|
||||
* @param integer $type 预载入方式 0 JOIN查询 1 IN查询
|
||||
* @return $this
|
||||
*/
|
||||
public function setEagerlyType($type)
|
||||
{
|
||||
@@ -167,8 +202,8 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 绑定关联表的属性到父模型属性
|
||||
* @access public
|
||||
* @param mixed $attr 要绑定的属性列表
|
||||
* @return this
|
||||
* @param mixed $attr 要绑定的属性列表
|
||||
* @return $this
|
||||
*/
|
||||
public function bind($attr)
|
||||
{
|
||||
@@ -182,19 +217,20 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 关联统计
|
||||
* @access public
|
||||
* @param Model $result 数据对象
|
||||
* @param \Closure $closure 闭包
|
||||
* @param Model $result 数据对象
|
||||
* @param \Closure $closure 闭包
|
||||
* @return integer
|
||||
*/
|
||||
public function relationCount($result, $closure)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 一对一 关联模型预查询拼装
|
||||
* @access public
|
||||
* @param string $model 模型名称
|
||||
* @param string $relation 关联名
|
||||
* @param Model $result 模型对象实例
|
||||
* @param string $model 模型名称
|
||||
* @param string $relation 关联名
|
||||
* @param Model $result 模型对象实例
|
||||
* @return void
|
||||
*/
|
||||
protected function match($model, $relation, &$result)
|
||||
@@ -215,16 +251,17 @@ abstract class OneToOne extends Relation
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
}
|
||||
$result->setAttr($relation, !isset($relationModel) ? null : $relationModel->isUpdate(true));
|
||||
$result->setAttr(Loader::parseName($relation), !isset($relationModel) ? null : $relationModel->isUpdate(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定关联属性到父模型
|
||||
* @access protected
|
||||
* @param Model $model 关联模型对象
|
||||
* @param Model $result 父模型对象
|
||||
* @param array $bindAttr 绑定属性
|
||||
* @param Model $model 关联模型对象
|
||||
* @param Model $result 父模型对象
|
||||
* @param array $bindAttr 绑定属性
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function bindAttr($model, &$result, $bindAttr)
|
||||
{
|
||||
@@ -241,26 +278,29 @@ abstract class OneToOne extends Relation
|
||||
/**
|
||||
* 一对一 关联模型预查询(IN方式)
|
||||
* @access public
|
||||
* @param object $model 关联模型对象
|
||||
* @param array $where 关联预查询条件
|
||||
* @param string $key 关联键名
|
||||
* @param string $relation 关联名
|
||||
* @param string $subRelation 子关联
|
||||
* @param bool $closure
|
||||
* @param object $model 关联模型对象
|
||||
* @param array $where 关联预查询条件
|
||||
* @param string $key 关联键名
|
||||
* @param string $relation 关联名
|
||||
* @param string $subRelation 子关联
|
||||
* @param bool|\Closure $closure
|
||||
* @return array
|
||||
*/
|
||||
protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false)
|
||||
{
|
||||
// 预载入关联查询 支持嵌套预载入
|
||||
if ($closure) {
|
||||
call_user_func_array($closure, [ & $model]);
|
||||
call_user_func_array($closure, [& $model]);
|
||||
if ($field = $model->getOptions('with_field')) {
|
||||
$model->field($field)->removeOption('with_field');
|
||||
}
|
||||
}
|
||||
$list = $model->where($where)->with($subRelation)->select();
|
||||
|
||||
// 组装模型数据
|
||||
$data = [];
|
||||
foreach ($list as $set) {
|
||||
$data[$set->$key][] = $set;
|
||||
$data[$set->$key] = $set;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user