This commit is contained in:
thinkphp
2017-01-23 13:58:41 +08:00
11 changed files with 402 additions and 340 deletions

View File

@@ -20,8 +20,6 @@ use think\model\Relation;
/**
* Class OneToOne
* @package think\model\relation
* @method void eagerlySet(array $resultSet, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据集)
* @method void eagerlyOne(Model $result, string $relation, string $subRelation, \Closure $closure) 预载入关联查询(数据)
*
*/
abstract class OneToOne extends Relation
@@ -36,7 +34,7 @@ abstract class OneToOne extends Relation
/**
* 设置join类型
* @access public
* @param string $type JOIN类型
* @param string $type JOIN类型
* @return $this
*/
public function joinType($type)
@@ -48,11 +46,11 @@ abstract class OneToOne extends Relation
/**
* 预载入关联查询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)
@@ -84,7 +82,7 @@ abstract class OneToOne extends Relation
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $query]);
call_user_func_array($closure, [& $query]);
// 使用withField指定获取关联的字段
// $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) {
@@ -99,13 +97,33 @@ 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 array $resultSet 数据集
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
@@ -124,10 +142,10 @@ abstract class OneToOne extends Relation
/**
* 预载入关联查询(数据)
* @access public
* @param Model $result 数据对象
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @param Model $result 数据对象
* @param string $relation 当前关联名
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
@@ -144,7 +162,7 @@ abstract class OneToOne extends Relation
/**
* 保存(新增)当前关联数据对象
* @access public
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @return integer
*/
public function save($data)
@@ -161,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)
{
@@ -184,8 +202,8 @@ abstract class OneToOne extends Relation
/**
* 绑定关联表的属性到父模型属性
* @access public
* @param mixed $attr 要绑定的属性列表
* @return this
* @param mixed $attr 要绑定的属性列表
* @return $this
*/
public function bind($attr)
{
@@ -199,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)
@@ -238,10 +257,11 @@ abstract class OneToOne extends Relation
/**
* 绑定关联属性到父模型
* @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)
{
@@ -258,19 +278,19 @@ 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');
}