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

@@ -27,18 +27,7 @@ use think\model\relation\MorphTo;
/** /**
* Class Model * Class Model
* @package think * @package think
* @method static Paginator paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询 * @mixin Query
* @method static mixed value($field, $default = null) 得到某个字段的值
* @method static array column($field, $key = '') 得到某个列的数组
* @method static integer count($field = '*') COUNT查询
* @method static integer sum($field = '*') SUM查询
* @method static integer min($field = '*') MIN查询
* @method static integer max($field = '*') MAX查询
* @method static integer avg($field = '*') AVG查询
* @method static setField($field, $value = '')
* @method static Query where($field, $op = null, $condition = null) 指定AND查询条件
* @method static static findOrFail($data = null) 查找单条记录 如果不存在则抛出异常
*
*/ */
abstract class Model implements \JsonSerializable, \ArrayAccess abstract class Model implements \JsonSerializable, \ArrayAccess
{ {
@@ -200,7 +189,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
// 全局作用域 // 全局作用域
if ($baseQuery && method_exists($this, 'base')) { if ($baseQuery && method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [ & self::$links[$model]]); call_user_func_array([$this, 'base'], [& self::$links[$model]]);
} }
// 返回当前模型的数据库查询对象 // 返回当前模型的数据库查询对象
return self::$links[$model]; return self::$links[$model];
@@ -226,7 +215,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @return void * @return void
*/ */
protected static function init() protected static function init()
{} {
}
/** /**
* 设置数据对象值 * 设置数据对象值
@@ -335,7 +325,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
break; break;
} }
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
'datetime',
'date',
'timestamp'
])
) {
$value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat);
} else { } else {
$value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat, true); $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat, true);
@@ -443,7 +438,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 类型转换 // 类型转换
$value = $this->readTransform($value, $this->type[$name]); $value = $this->readTransform($value, $this->type[$name]);
} elseif (in_array($name, [$this->createTime, $this->updateTime])) { } elseif (in_array($name, [$this->createTime, $this->updateTime])) {
if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
'datetime',
'date',
'timestamp'
])
) {
$value = $this->formatDateTime(strtotime($value), $this->dateFormat); $value = $this->formatDateTime(strtotime($value), $this->dateFormat);
} else { } else {
$value = $this->formatDateTime($value, $this->dateFormat); $value = $this->formatDateTime($value, $this->dateFormat);
@@ -544,6 +544,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $relation 关联方法 * @param string $relation 关联方法
* @param string|array $append 追加属性名 * @param string|array $append 追加属性名
* @return $this * @return $this
* @throws Exception
*/ */
public function appendRelationAttr($relation, $append) public function appendRelationAttr($relation, $append)
{ {
@@ -965,6 +966,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $dataSet 数据 * @param array $dataSet 数据
* @param boolean $replace 是否自动识别更新和写入 * @param boolean $replace 是否自动识别更新和写入
* @return array|false * @return array|false
* @throws \Exception
*/ */
public function saveAll($dataSet, $replace = true) public function saveAll($dataSet, $replace = true)
{ {
@@ -1217,7 +1219,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (isset(self::$event[$this->class][$event])) { if (isset(self::$event[$this->class][$event])) {
foreach (self::$event[$this->class][$event] as $callback) { foreach (self::$event[$this->class][$event] as $callback) {
if (is_callable($callback)) { if (is_callable($callback)) {
$result = call_user_func_array($callback, [ & $params]); $result = call_user_func_array($callback, [& $params]);
if (false === $result) { if (false === $result) {
return false; return false;
} }
@@ -1307,7 +1309,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = $result->where($data); $result = $result->where($data);
$data = null; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $result]); call_user_func_array($data, [& $result]);
$data = null; $data = null;
} elseif ($data instanceof Query) { } elseif ($data instanceof Query) {
$result = $data->with($with)->cache($cache); $result = $data->with($with)->cache($cache);
@@ -1330,7 +1332,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query->where($data); $query->where($data);
$data = null; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]); call_user_func_array($data, [& $query]);
$data = null; $data = null;
} elseif (is_null($data)) { } elseif (is_null($data)) {
return 0; return 0;
@@ -1349,9 +1351,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 命名范围 * 命名范围
* @access public * @access public
* @param string|array|Closure $name 命名范围名称 逗号分隔 * @param string|array|\Closure $name 命名范围名称 逗号分隔
* @param mixed ...$params 参数调用 * @internal mixed ...$params 参数调用
* @return Model * @return Model|Query
*/ */
public static function scope($name) public static function scope($name)
{ {
@@ -1829,6 +1831,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 模型事件快捷方法 * 模型事件快捷方法
* @param $callback
* @param bool $override
*/ */
protected static function beforeInsert($callback, $override = false) protected static function beforeInsert($callback, $override = false)
{ {

View File

@@ -11,7 +11,6 @@
namespace think\model; namespace think\model;
use think\Db;
use think\db\Query; use think\db\Query;
use think\Model; use think\Model;
@@ -41,7 +40,7 @@ class Merge extends Model
* 查找单条记录 * 查找单条记录
* @access public * @access public
* @param mixed $data 主键值或者查询条件(闭包) * @param mixed $data 主键值或者查询条件(闭包)
* @param string $with 关联预查询 * @param string|array $with 关联预查询
* @param bool $cache 是否缓存 * @param bool $cache 是否缓存
* @return \think\Model * @return \think\Model
*/ */
@@ -105,7 +104,8 @@ class Merge extends Model
* 查找所有记录 * 查找所有记录
* @access public * @access public
* @param mixed $data 主键列表或者查询条件(闭包) * @param mixed $data 主键列表或者查询条件(闭包)
* @param string $with 关联预查询 * @param array|string $with 关联预查询
* @param bool $cache
* @return array|false|string * @return array|false|string
*/ */
public static function all($data = null, $with = [], $cache = false) public static function all($data = null, $with = [], $cache = false)
@@ -121,7 +121,7 @@ class Merge extends Model
* @param string $model 模型名称 * @param string $model 模型名称
* @param array $data 数据 * @param array $data 数据
* @param bool $insert 是否新增 * @param bool $insert 是否新增
* @return void * @return array
*/ */
protected function parseData($model, $data, $insert = false) protected function parseData($model, $data, $insert = false)
{ {
@@ -147,7 +147,8 @@ class Merge extends Model
* @param mixed $data 数据 * @param mixed $data 数据
* @param array $where 更新条件 * @param array $where 更新条件
* @param string $sequence 自增序列名 * @param string $sequence 自增序列名
* @return integer|false * @return false|int
* @throws \Exception
*/ */
public function save($data = [], $where = [], $sequence = null) public function save($data = [], $where = [], $sequence = null)
{ {
@@ -278,7 +279,8 @@ class Merge extends Model
/** /**
* 删除当前的记录 并删除关联数据 * 删除当前的记录 并删除关联数据
* @access public * @access public
* @return integer * @return int
* @throws \Exception
*/ */
public function delete() public function delete()
{ {

View File

@@ -15,13 +15,19 @@ use think\db\Query;
use think\Exception; use think\Exception;
use think\Model; use think\Model;
/**
* Class Relation
* @package think\model
*
* @mixin Query
*/
abstract class Relation abstract class Relation
{ {
// 父模型对象 // 父模型对象
protected $parent; protected $parent;
/** @var Model 当前关联的模型类 */ /** @var Model 当前关联的模型类 */
protected $model; protected $model;
// 关联模型查询对象 /** @var Query 关联模型查询对象 */
protected $query; protected $query;
// 关联表外键 // 关联表外键
protected $foreignKey; protected $foreignKey;
@@ -84,6 +90,13 @@ abstract class Relation
return $this; return $this;
} }
/**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
abstract protected function baseQuery();
public function __call($method, $args) public function __call($method, $args)
{ {
if ($this->query) { if ($this->query) {

View File

@@ -40,6 +40,7 @@ class BelongsTo extends OneToOne
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @access public
* @return array|false|\PDOStatement|string|Model
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {

View File

@@ -11,7 +11,6 @@
namespace think\model\relation; namespace think\model\relation;
use think\Db;
use think\db\Query; use think\db\Query;
use think\Exception; use think\Exception;
use think\Loader; use think\Loader;
@@ -47,7 +46,7 @@ class BelongsToMany extends Relation
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return false|\PDOStatement|string|\think\Collection
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
@@ -55,7 +54,7 @@ class BelongsToMany extends Relation
$localKey = $this->localKey; $localKey = $this->localKey;
$middle = $this->middle; $middle = $this->middle;
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
// 关联查询 // 关联查询
$pk = $this->parent->getPk(); $pk = $this->parent->getPk();
@@ -172,7 +171,12 @@ class BelongsToMany extends Relation
*/ */
public function getRelationCountQuery($closure) public function getRelationCountQuery($closure)
{ {
return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [
'pivot.' . $this->localKey => [
'exp',
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
]
])->fetchSql()->count();
} }
/** /**
@@ -267,7 +271,8 @@ class BelongsToMany extends Relation
* @access public * @access public
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键 * @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
* @param array $pivot 中间表额外数据 * @param array $pivot 中间表额外数据
* @return integer * @return int
* @throws Exception
*/ */
public function attach($data, $pivot = []) public function attach($data, $pivot = [])
{ {

View File

@@ -40,12 +40,12 @@ class HasMany extends Relation
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return false|\PDOStatement|string|\think\Collection
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
return $this->relation($subRelation)->select(); return $this->relation($subRelation)->select();
} }
@@ -57,7 +57,6 @@ class HasMany extends Relation
* @param string $relation 当前关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包 * @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void * @return void
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
@@ -97,7 +96,6 @@ class HasMany extends Relation
* @param string $relation 当前关联名 * @param string $relation 当前关联名
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包 * @param \Closure $closure 闭包
* @param string $class 数据集对象名 为空表示数组
* @return void * @return void
*/ */
public function eagerlyResult(&$result, $relation, $subRelation, $closure) public function eagerlyResult(&$result, $relation, $subRelation, $closure)
@@ -127,7 +125,7 @@ class HasMany extends Relation
$count = 0; $count = 0;
if (isset($result->$localKey)) { if (isset($result->$localKey)) {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
$count = $this->query->where([$this->foreignKey => $result->$localKey])->count(); $count = $this->query->where([$this->foreignKey => $result->$localKey])->count();
} }
@@ -143,10 +141,15 @@ class HasMany extends Relation
public function getRelationCountQuery($closure) public function getRelationCountQuery($closure)
{ {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
return $this->query->where([$this->foreignKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); return $this->query->where([
$this->foreignKey => [
'exp',
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
]
])->fetchSql()->count();
} }
/** /**
@@ -164,7 +167,7 @@ class HasMany extends Relation
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
// 预载入关联查询 支持嵌套预载入 // 预载入关联查询 支持嵌套预载入
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $model]); call_user_func_array($closure, [& $model]);
} }
$list = $model->where($where)->with($subRelation)->select(); $list = $model->where($where)->with($subRelation)->select();

View File

@@ -30,8 +30,8 @@ class HasManyThrough extends Relation
* @param Model $parent 上级模型对象 * @param Model $parent 上级模型对象
* @param string $model 模型名 * @param string $model 模型名
* @param string $through 中间模型名 * @param string $through 中间模型名
* @param string $firstkey 关联外键 * @param string $foreignKey 关联外键
* @param string $secondKey 关联外键 * @param string $throughKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
*/ */
public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey) public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey)
@@ -49,12 +49,12 @@ class HasManyThrough extends Relation
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return false|\PDOStatement|string|\think\Collection
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
return $this->relation($subRelation)->select(); return $this->relation($subRelation)->select();
} }
@@ -70,7 +70,8 @@ class HasManyThrough extends Relation
* @return void * @return void
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{} {
}
/** /**
* 预载入关联查询 返回模型对象 * 预载入关联查询 返回模型对象
@@ -83,7 +84,8 @@ class HasManyThrough extends Relation
* @return void * @return void
*/ */
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{} {
}
/** /**
* 关联统计 * 关联统计
@@ -93,7 +95,8 @@ class HasManyThrough extends Relation
* @return integer * @return integer
*/ */
public function relationCount($result, $closure) public function relationCount($result, $closure)
{} {
}
/** /**
* 执行基础查询(进执行一次) * 执行基础查询(进执行一次)

View File

@@ -11,6 +11,7 @@
namespace think\model\relation; namespace think\model\relation;
use think\db\Query;
use think\Loader; use think\Loader;
use think\Model; use think\Model;
@@ -39,14 +40,14 @@ class HasOne extends OneToOne
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return array|false|\PDOStatement|string|Model
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
// 执行关联定义方法 // 执行关联定义方法
$localKey = $this->localKey; $localKey = $this->localKey;
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
// 判断关联类型执行查询 // 判断关联类型执行查询
return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();

View File

@@ -48,12 +48,12 @@ class MorphMany extends Relation
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return false|\PDOStatement|string|\think\Collection
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
return $this->relation($subRelation)->select(); return $this->relation($subRelation)->select();
} }
@@ -111,7 +111,10 @@ class MorphMany extends Relation
{ {
$pk = $result->getPk(); $pk = $result->getPk();
if (isset($result->$pk)) { if (isset($result->$pk)) {
$data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); $data = $this->eagerlyMorphToMany([
$this->morphKey => $result->$pk,
$this->morphType => $this->type
], $relation, $subRelation, $closure);
$result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk]));
} }
} }
@@ -129,7 +132,7 @@ class MorphMany extends Relation
$count = 0; $count = 0;
if (isset($result->$pk)) { if (isset($result->$pk)) {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
$count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count(); $count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count();
} }
@@ -145,27 +148,32 @@ class MorphMany extends Relation
public function getRelationCountQuery($closure) public function getRelationCountQuery($closure)
{ {
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [& $this->query]);
} }
return $this->query->where([$this->morphKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()], $this->morphType => $this->type])->fetchSql()->count(); return $this->query->where([
$this->morphKey => [
'exp',
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
],
$this->morphType => $this->type
])->fetchSql()->count();
} }
/** /**
* 多态一对多 关联模型预查询 * 多态一对多 关联模型预查询
* @access public * @access public
* @param object $model 关联模型对象
* @param array $where 关联预查询条件 * @param array $where 关联预查询条件
* @param string $relation 关联名 * @param string $relation 关联名
* @param string $subRelation 子关联 * @param string $subRelation 子关联
* @param \Closure $closure 闭包 * @param bool|\Closure $closure 闭包
* @return array * @return array
*/ */
protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false) protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false)
{ {
// 预载入关联查询 支持嵌套预载入 // 预载入关联查询 支持嵌套预载入
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $this]); call_user_func_array($closure, [& $this]);
} }
$list = $this->query->where($where)->with($subRelation)->select(); $list = $this->query->where($where)->with($subRelation)->select();
$morphKey = $this->morphKey; $morphKey = $this->morphKey;

View File

@@ -44,7 +44,7 @@ class MorphTo extends Relation
* 延迟获取关联数据 * 延迟获取关联数据
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件 * @param \Closure $closure 闭包查询条件
* @access public * @return mixed
*/ */
public function getRelation($subRelation = '', $closure = null) public function getRelation($subRelation = '', $closure = null)
{ {
@@ -97,6 +97,7 @@ class MorphTo extends Relation
* @param string $subRelation 子关联名 * @param string $subRelation 子关联名
* @param \Closure $closure 闭包 * @param \Closure $closure 闭包
* @return void * @return void
* @throws Exception
*/ */
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
{ {
@@ -163,14 +164,15 @@ class MorphTo extends Relation
* @return integer * @return integer
*/ */
public function relationCount($result, $closure) public function relationCount($result, $closure)
{} {
}
/** /**
* 多态MorphTo 关联模型预查询 * 多态MorphTo 关联模型预查询
* @access public * @access public
* @param object $model 关联模型对象 * @param object $model 关联模型对象
* @param array $where 关联预查询条件
* @param string $relation 关联名 * @param string $relation 关联名
* @param $result
* @param string $subRelation 子关联 * @param string $subRelation 子关联
* @return void * @return void
*/ */

View File

@@ -20,8 +20,6 @@ use think\model\Relation;
/** /**
* Class OneToOne * Class OneToOne
* @package think\model\relation * @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 abstract class OneToOne extends Relation
@@ -84,7 +82,7 @@ abstract class OneToOne extends Relation
if ($closure) { if ($closure) {
// 执行闭包查询 // 执行闭包查询
call_user_func_array($closure, [ & $query]); call_user_func_array($closure, [& $query]);
// 使用withField指定获取关联的字段 // 使用withField指定获取关联的字段
// $query->where(['id'=>1])->withField('id,name'); // $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) { if ($query->getOptions('with_field')) {
@@ -99,6 +97,26 @@ abstract class OneToOne extends Relation
$query->field($field, false, $joinTable, $joinAlias, $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 * @access public
@@ -162,7 +180,7 @@ abstract class OneToOne extends Relation
* 设置预载入方式 * 设置预载入方式
* @access public * @access public
* @param integer $type 预载入方式 0 JOIN查询 1 IN查询 * @param integer $type 预载入方式 0 JOIN查询 1 IN查询
* @return this * @return $this
*/ */
public function setEagerlyType($type) public function setEagerlyType($type)
{ {
@@ -185,7 +203,7 @@ abstract class OneToOne extends Relation
* 绑定关联表的属性到父模型属性 * 绑定关联表的属性到父模型属性
* @access public * @access public
* @param mixed $attr 要绑定的属性列表 * @param mixed $attr 要绑定的属性列表
* @return this * @return $this
*/ */
public function bind($attr) public function bind($attr)
{ {
@@ -204,7 +222,8 @@ abstract class OneToOne extends Relation
* @return integer * @return integer
*/ */
public function relationCount($result, $closure) public function relationCount($result, $closure)
{} {
}
/** /**
* 一对一 关联模型预查询拼装 * 一对一 关联模型预查询拼装
@@ -242,6 +261,7 @@ abstract class OneToOne extends Relation
* @param Model $result 父模型对象 * @param Model $result 父模型对象
* @param array $bindAttr 绑定属性 * @param array $bindAttr 绑定属性
* @return void * @return void
* @throws Exception
*/ */
protected function bindAttr($model, &$result, $bindAttr) protected function bindAttr($model, &$result, $bindAttr)
{ {
@@ -263,14 +283,14 @@ abstract class OneToOne extends Relation
* @param string $key 关联键名 * @param string $key 关联键名
* @param string $relation 关联名 * @param string $relation 关联名
* @param string $subRelation 子关联 * @param string $subRelation 子关联
* @param bool $closure * @param bool|\Closure $closure
* @return array * @return array
*/ */
protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false) protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false)
{ {
// 预载入关联查询 支持嵌套预载入 // 预载入关联查询 支持嵌套预载入
if ($closure) { if ($closure) {
call_user_func_array($closure, [ & $model]); call_user_func_array($closure, [& $model]);
if ($field = $model->getOptions('with_field')) { if ($field = $model->getOptions('with_field')) {
$model->field($field)->removeOption('with_field'); $model->field($field)->removeOption('with_field');
} }