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
* @package think
* @method static Paginator paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询
* @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) 查找单条记录 如果不存在则抛出异常
*
* @mixin Query
*/
abstract class Model implements \JsonSerializable, \ArrayAccess
{
@@ -200,7 +189,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
// 全局作用域
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];
@@ -226,7 +215,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @return void
*/
protected static function init()
{}
{
}
/**
* 设置数据对象值
@@ -335,7 +325,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $_SERVER['REQUEST_TIME'];
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);
} else {
$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]);
} 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);
} else {
$value = $this->formatDateTime($value, $this->dateFormat);
@@ -544,6 +544,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $relation 关联方法
* @param string|array $append 追加属性名
* @return $this
* @throws Exception
*/
public function appendRelationAttr($relation, $append)
{
@@ -965,6 +966,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $dataSet 数据
* @param boolean $replace 是否自动识别更新和写入
* @return array|false
* @throws \Exception
*/
public function saveAll($dataSet, $replace = true)
{
@@ -1217,7 +1219,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (isset(self::$event[$this->class][$event])) {
foreach (self::$event[$this->class][$event] as $callback) {
if (is_callable($callback)) {
$result = call_user_func_array($callback, [ & $params]);
$result = call_user_func_array($callback, [& $params]);
if (false === $result) {
return false;
}
@@ -1307,7 +1309,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = $result->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $result]);
call_user_func_array($data, [& $result]);
$data = null;
} elseif ($data instanceof Query) {
$result = $data->with($with)->cache($cache);
@@ -1330,7 +1332,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
call_user_func_array($data, [& $query]);
$data = null;
} elseif (is_null($data)) {
return 0;
@@ -1349,9 +1351,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/**
* 命名范围
* @access public
* @param string|array|Closure $name 命名范围名称 逗号分隔
* @param mixed ...$params 参数调用
* @return Model
* @param string|array|\Closure $name 命名范围名称 逗号分隔
* @internal mixed ...$params 参数调用
* @return Model|Query
*/
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)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,6 +11,7 @@
namespace think\model\relation;
use think\db\Query;
use think\Loader;
use think\Model;
@@ -39,14 +40,14 @@ class HasOne extends OneToOne
* 延迟获取关联数据
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件
* @access public
* @return array|false|\PDOStatement|string|Model
*/
public function getRelation($subRelation = '', $closure = null)
{
// 执行关联定义方法
$localKey = $this->localKey;
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();

View File

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

View File

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

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
@@ -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,6 +97,26 @@ 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
@@ -162,7 +180,7 @@ abstract class OneToOne extends Relation
* 设置预载入方式
* @access public
* @param integer $type 预载入方式 0 JOIN查询 1 IN查询
* @return this
* @return $this
*/
public function setEagerlyType($type)
{
@@ -185,7 +203,7 @@ abstract class OneToOne extends Relation
* 绑定关联表的属性到父模型属性
* @access public
* @param mixed $attr 要绑定的属性列表
* @return this
* @return $this
*/
public function bind($attr)
{
@@ -204,7 +222,8 @@ abstract class OneToOne extends Relation
* @return integer
*/
public function relationCount($result, $closure)
{}
{
}
/**
* 一对一 关联模型预查询拼装
@@ -242,6 +261,7 @@ abstract class OneToOne extends Relation
* @param Model $result 父模型对象
* @param array $bindAttr 绑定属性
* @return void
* @throws Exception
*/
protected function bindAttr($model, &$result, $bindAttr)
{
@@ -263,14 +283,14 @@ abstract class OneToOne extends Relation
* @param string $key 关联键名
* @param string $relation 关联名
* @param string $subRelation 子关联
* @param bool $closure
* @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');
}