Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 23, 2017
2 parents c270a32 + 6078d0e commit 390aeb3
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 340 deletions.
214 changes: 109 additions & 105 deletions library/think/Model.php

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions library/think/model/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace think\model;

use think\Db;
use think\db\Query;
use think\Model;

Expand Down Expand Up @@ -40,9 +39,9 @@ public function __construct($data = [])
/**
* 查找单条记录
* @access public
* @param mixed $data 主键值或者查询条件(闭包)
* @param string $with 关联预查询
* @param bool $cache 是否缓存
* @param mixed $data 主键值或者查询条件(闭包)
* @param string|array $with 关联预查询
* @param bool $cache 是否缓存
* @return \think\Model
*/
public static function get($data = null, $with = [], $cache = false)
Expand Down Expand Up @@ -78,11 +77,11 @@ protected static function attachQuery($query)
/**
* 获取关联模型的字段 并解决混淆
* @access protected
* @param \think\db\Query $query 查询对象
* @param string $name 模型名称
* @param string $table 关联表名称
* @param array $map 字段映射
* @param array $fields 查询字段
* @param \think\db\Query $query 查询对象
* @param string $name 模型名称
* @param string $table 关联表名称
* @param array $map 字段映射
* @param array $fields 查询字段
* @return array
*/
protected static function getModelField($query, $name, $table = '', $map = [], $fields = [])
Expand All @@ -104,8 +103,9 @@ protected static function getModelField($query, $name, $table = '', $map = [], $
/**
* 查找所有记录
* @access public
* @param mixed $data 主键列表或者查询条件(闭包)
* @param string $with 关联预查询
* @param mixed $data 主键列表或者查询条件(闭包)
* @param array|string $with 关联预查询
* @param bool $cache
* @return array|false|string
*/
public static function all($data = null, $with = [], $cache = false)
Expand All @@ -118,10 +118,10 @@ public static function all($data = null, $with = [], $cache = false)
/**
* 处理写入的模型数据
* @access public
* @param string $model 模型名称
* @param array $data 数据
* @param bool $insert 是否新增
* @return void
* @param string $model 模型名称
* @param array $data 数据
* @param bool $insert 是否新增
* @return array
*/
protected function parseData($model, $data, $insert = false)
{
Expand All @@ -144,10 +144,11 @@ protected function parseData($model, $data, $insert = false)
/**
* 保存模型数据 以及关联数据
* @access public
* @param mixed $data 数据
* @param array $where 更新条件
* @param string $sequence 自增序列名
* @return integer|false
* @param mixed $data 数据
* @param array $where 更新条件
* @param string $sequence 自增序列名
* @return false|int
* @throws \Exception
*/
public function save($data = [], $where = [], $sequence = null)
{
Expand Down Expand Up @@ -278,7 +279,8 @@ public function save($data = [], $where = [], $sequence = null)
/**
* 删除当前的记录 并删除关联数据
* @access public
* @return integer
* @return int
* @throws \Exception
*/
public function delete()
{
Expand Down
17 changes: 15 additions & 2 deletions library/think/model/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
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;
Expand Down Expand Up @@ -65,7 +71,7 @@ public function getQuery()
/**
* 封装关联数据集
* @access public
* @param array $resultSet 数据集
* @param array $resultSet 数据集
* @return mixed
*/
protected function resultSetBuild($resultSet)
Expand All @@ -84,6 +90,13 @@ public function removeOption()
return $this;
}

/**
* 执行基础查询(进执行一次)
* @access protected
* @return void
*/
abstract protected function baseQuery();

public function __call($method, $args)
{
if ($this->query) {
Expand Down
5 changes: 3 additions & 2 deletions library/think/model/relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function __construct(Model $parent, $model, $foreignKey, $localKey, $join

/**
* 延迟获取关联数据
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件
* @access public
* @return array|false|\PDOStatement|string|Model
*/
public function getRelation($subRelation = '', $closure = null)
{
Expand Down
81 changes: 43 additions & 38 deletions library/think/model/relation/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace think\model\relation;

use think\Db;
use think\db\Query;
use think\Exception;
use think\Loader;
Expand All @@ -27,11 +26,11 @@ class BelongsToMany extends Relation
/**
* 架构函数
* @access public
* @param Model $parent 上级模型对象
* @param string $model 模型名
* @param string $table 中间表名
* @param Model $parent 上级模型对象
* @param string $model 模型名
* @param string $table 中间表名
* @param string $foreignKey 关联模型外键
* @param string $localKey 当前模型关联键
* @param string $localKey 当前模型关联键
*/
public function __construct(Model $parent, $model, $table, $foreignKey, $localKey)
{
Expand All @@ -45,17 +44,17 @@ public function __construct(Model $parent, $model, $table, $foreignKey, $localKe

/**
* 延迟获取关联数据
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件
* @access public
* @param string $subRelation 子关联名
* @param \Closure $closure 闭包查询条件
* @return false|\PDOStatement|string|\think\Collection
*/
public function getRelation($subRelation = '', $closure = null)
{
$foreignKey = $this->foreignKey;
$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();
Expand All @@ -80,10 +79,10 @@ public function getRelation($subRelation = '', $closure = null)
/**
* 预载入关联查询(数据集)
* @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)
Expand Down Expand Up @@ -124,10 +123,10 @@ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
/**
* 预载入关联查询(单个数据)
* @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)
Expand All @@ -149,8 +148,8 @@ public function eagerlyResult(&$result, $relation, $subRelation, $closure)
/**
* 关联统计
* @access public
* @param Model $result 数据对象
* @param \Closure $closure 闭包
* @param Model $result 数据对象
* @param \Closure $closure 闭包
* @return integer
*/
public function relationCount($result, $closure)
Expand All @@ -167,20 +166,25 @@ public function relationCount($result, $closure)
/**
* 获取关联统计子查询
* @access public
* @param \Closure $closure 闭包
* @param \Closure $closure 闭包
* @return string
*/
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();
}

/**
* 多对多 关联模型预查询
* @access public
* @param array $where 关联预查询条件
* @param string $relation 关联名
* @param string $subRelation 子关联
* @param array $where 关联预查询条件
* @param string $relation 关联名
* @param string $subRelation 子关联
* @return array
*/
protected function eagerlyManyToMany($where, $relation, $subRelation = '')
Expand Down Expand Up @@ -210,10 +214,10 @@ protected function eagerlyManyToMany($where, $relation, $subRelation = '')
/**
* BELONGS TO MANY 关联查询
* @access public
* @param string $table 中间表名
* @param string $foreignKey 关联模型关联键
* @param string $localKey 当前模型关联键
* @param array $condition 关联查询条件
* @param string $table 中间表名
* @param string $foreignKey 关联模型关联键
* @param string $localKey 当前模型关联键
* @param array $condition 关联查询条件
* @return Query
*/
protected function belongsToManyQuery($table, $foreignKey, $localKey, $condition = [])
Expand All @@ -230,8 +234,8 @@ protected function belongsToManyQuery($table, $foreignKey, $localKey, $condition
/**
* 保存(新增)当前关联数据对象
* @access public
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @param array $pivot 中间表额外数据
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
* @param array $pivot 中间表额外数据
* @return integer
*/
public function save($data, array $pivot = [])
Expand All @@ -243,9 +247,9 @@ public function save($data, array $pivot = [])
/**
* 批量保存当前关联数据对象
* @access public
* @param array $dataSet 数据集
* @param array $pivot 中间表额外数据
* @param bool $samePivot 额外数据是否相同
* @param array $dataSet 数据集
* @param array $pivot 中间表额外数据
* @param bool $samePivot 额外数据是否相同
* @return integer
*/
public function saveAll(array $dataSet, array $pivot = [], $samePivot = false)
Expand All @@ -265,9 +269,10 @@ public function saveAll(array $dataSet, array $pivot = [], $samePivot = false)
/**
* 附加关联的一个中间表数据
* @access public
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
* @param array $pivot 中间表额外数据
* @return integer
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
* @param array $pivot 中间表额外数据
* @return int
* @throws Exception
*/
public function attach($data, $pivot = [])
{
Expand Down Expand Up @@ -307,8 +312,8 @@ public function attach($data, $pivot = [])
/**
* 解除关联的一个中间表数据
* @access public
* @param integer|array $data 数据 可以使用关联对象的主键
* @param bool $relationDel 是否同时删除关联表数据
* @param integer|array $data 数据 可以使用关联对象的主键
* @param bool $relationDel 是否同时删除关联表数据
* @return integer
*/
public function detach($data = null, $relationDel = false)
Expand Down
Loading

0 comments on commit 390aeb3

Please sign in to comment.