改进Relation类 支持关联的新增

This commit is contained in:
thinkphp
2016-04-20 08:33:23 +08:00
parent dc40fac651
commit b599829e75
3 changed files with 73 additions and 20 deletions

View File

@@ -66,6 +66,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $isUpdate = false; protected $isUpdate = false;
// 当前执行的关联对象 // 当前执行的关联对象
protected $relation; protected $relation;
// 当前模型的父模型
protected $parent;
/** /**
* 初始化过的模型. * 初始化过的模型.
@@ -329,7 +331,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $where 更新条件 * @param array $where 更新条件
* @return integer * @return integer
*/ */
public function save($data = [], $where = []) public function save($data = [], $where = [], $getInsertId = true)
{ {
if (!empty($data)) { if (!empty($data)) {
// 数据对象赋值 // 数据对象赋值
@@ -392,7 +394,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = self::db()->insert($this->data); $result = self::db()->insert($this->data);
// 获取自动增长主键 // 获取自动增长主键
if ($result) { if ($result && $getInsertId) {
$insertId = self::db()->getLastInsID(); $insertId = self::db()->getLastInsID();
if (is_string($this->pk) && $insertId) { if (is_string($this->pk) && $insertId) {
$this->data[$this->pk] = $insertId; $this->data[$this->pk] = $insertId;
@@ -420,7 +422,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function saveAll($dataSet) public function saveAll($dataSet)
{ {
foreach ($dataSet as $data) { foreach ($dataSet as $data) {
$result = $this->save($data); $result = $this->isUpdate(false)->save($data, [], false);
} }
return $result; return $result;
} }
@@ -858,10 +860,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 初始化数据库对象 * 初始化数据库对象
* @access protected * @access public
* @return \think\db\Driver * @return \think\db\Driver
*/ */
protected static function db() public static function db()
{ {
$model = get_called_class(); $model = get_called_class();

View File

@@ -850,8 +850,8 @@ class Query
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName); $this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
} }
// 预载入封装 // 预载入封装
$table = $model::getTableName(); $table = $info['model']::getTableName();
$name = strtolower(basename(str_replace('\\', '/', $model))); $name = strtolower(basename(str_replace('\\', '/', $info['model'])));
$this->via($name); $this->via($name);
$this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__'); $this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__');
if ($closure) { if ($closure) {

View File

@@ -41,6 +41,7 @@ class Relation
public function __construct($model) public function __construct($model)
{ {
$this->parent = $model; $this->parent = $model;
$this->db = $model->db();
} }
/** /**
@@ -62,17 +63,17 @@ class Relation
} }
// 获取关联数据 // 获取关联数据
public function getRelation($relation) public function getRelation($name)
{ {
// 执行关联定义方法 // 执行关联定义方法
$model = $this->parent->$relation(); $relation = $this->parent->$name();
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
$localKey = $this->localKey; $localKey = $this->localKey;
// 判断关联类型执行查询 // 判断关联类型执行查询
switch ($this->type) { switch ($this->type) {
case self::HAS_ONE: case self::HAS_ONE:
case self::BELONGS_TO: case self::BELONGS_TO:
$result = $model::where($foreignKey, $this->parent->$localKey)->find(); $result = $relation->where($foreignKey, $this->parent->$localKey)->find();
if (false === $result) { if (false === $result) {
$class = $this->model; $class = $this->model;
$result = new $class; $result = new $class;
@@ -80,7 +81,7 @@ class Relation
} }
break; break;
case self::HAS_MANY: case self::HAS_MANY:
$result = $model::where($foreignKey, $this->parent->$localKey)->select(); $result = $relation->where($foreignKey, $this->parent->$localKey)->select();
break; break;
case self::BELONGS_TO_MANY: case self::BELONGS_TO_MANY:
// 关联查询 // 关联查询
@@ -147,7 +148,7 @@ class Relation
} }
if (!empty($range)) { if (!empty($range)) {
$data = $this->eagerlyOneToMany($model, [$foreignKey => ['in', $range]], $relation, $subRelation); $data = $this->eagerlyOneToMany($this->model, [$foreignKey => ['in', $range]], $relation, $subRelation);
// 关联数据封装 // 关联数据封装
foreach ($resultSet as $result) { foreach ($resultSet as $result) {
@@ -171,7 +172,7 @@ class Relation
if (!empty($range)) { if (!empty($range)) {
// 查询关联数据 // 查询关联数据
$data = $this->eagerlyManyToMany($model, ['pivot.' . $foreignKey => ['in', $range]], $relation, $subRelation); $data = $this->eagerlyManyToMany($this->model, ['pivot.' . $foreignKey => ['in', $range]], $relation, $subRelation);
// 关联数据封装 // 关联数据封装
foreach ($resultSet as $result) { foreach ($resultSet as $result) {
@@ -213,7 +214,7 @@ class Relation
case self::HAS_ONE: case self::HAS_ONE:
case self::BELONGS_TO: case self::BELONGS_TO:
// 模型关联组装 // 模型关联组装
$this->match($model, $relation, $result); $this->match($this->model, $relation, $result);
break; break;
case self::HAS_MANY: case self::HAS_MANY:
if (isset($result->$localKey)) { if (isset($result->$localKey)) {
@@ -230,7 +231,7 @@ class Relation
if (isset($result->$pk)) { if (isset($result->$pk)) {
$pk = $result->$pk; $pk = $result->$pk;
// 查询管理数据 // 查询管理数据
$data = $this->eagerlyManyToMany($model, ['pivot.' . $foreignKey => $pk], $relation, $subRelation); $data = $this->eagerlyManyToMany($this->model, ['pivot.' . $foreignKey => $pk], $relation, $subRelation);
// 关联数据封装 // 关联数据封装
if (!isset($data[$pk])) { if (!isset($data[$pk])) {
@@ -287,7 +288,7 @@ class Relation
{ {
$foreignKey = $this->foreignKey; $foreignKey = $this->foreignKey;
// 预载入关联查询 支持嵌套预载入 // 预载入关联查询 支持嵌套预载入
$list = $model::where($where)->with($subRelation)->select(); $list = $model->where($where)->with($subRelation)->select();
// 组装模型数据 // 组装模型数据
$data = []; $data = [];
@@ -348,7 +349,7 @@ class Relation
$this->localKey = $localKey; $this->localKey = $localKey;
// 返回关联的模型对象 // 返回关联的模型对象
return new $model; return $this;
} }
/** /**
@@ -368,7 +369,7 @@ class Relation
$this->localKey = $localKey; $this->localKey = $localKey;
// 返回关联的模型对象 // 返回关联的模型对象
return new $model; return $this;
} }
/** /**
@@ -388,7 +389,7 @@ class Relation
$this->localKey = $localKey; $this->localKey = $localKey;
// 返回关联的模型对象 // 返回关联的模型对象
return new $model; return $this;
} }
/** /**
@@ -410,7 +411,7 @@ class Relation
$this->middle = $table; $this->middle = $table;
// 返回关联的模型对象 // 返回关联的模型对象
return new $model; return $this;
} }
/** /**
@@ -434,4 +435,54 @@ class Relation
->where($condition); ->where($condition);
} }
/**
* 保存当前关联数据对象
* @access public
* @param array $data 数据
* @return integer
*/
public function save($data, $pivot = [])
{
// 判断关联类型执行查询
switch ($this->type) {
case self::HAS_ONE:
case self::BELONGS_TO:
case self::HAS_MANY:
$data[$this->foreignKey] = $this->parent->{$this->localKey};
break;
case self::BELONGS_TO_MANY:
break;
}
$model = new $this->model;
return $model->save($data);
}
/**
* 保存当前关联数据对象
* @access public
* @param array $data 数据
* @return integer
*/
public function saveAll($dataSet, $pivot = [])
{
foreach ($dataSet as $data) {
// 判断关联类型执行查询
switch ($this->type) {
case self::HAS_MANY:
$data[$this->foreignKey] = $this->parent->{$this->localKey};
break;
case self::BELONGS_TO_MANY:
break;
}
}
$model = new $this->model;
return $model->saveAll($dataSet);
}
public function __call($method, $args)
{
$model = new $this->model;
return call_user_func_array([$model->db(), $method], $args);
}
} }