mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
改进Relation类 支持关联的新增
This commit is contained in:
@@ -66,6 +66,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
protected $isUpdate = false;
|
||||
// 当前执行的关联对象
|
||||
protected $relation;
|
||||
// 当前模型的父模型
|
||||
protected $parent;
|
||||
|
||||
/**
|
||||
* 初始化过的模型.
|
||||
@@ -329,7 +331,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
* @param array $where 更新条件
|
||||
* @return integer
|
||||
*/
|
||||
public function save($data = [], $where = [])
|
||||
public function save($data = [], $where = [], $getInsertId = true)
|
||||
{
|
||||
if (!empty($data)) {
|
||||
// 数据对象赋值
|
||||
@@ -392,7 +394,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$result = self::db()->insert($this->data);
|
||||
|
||||
// 获取自动增长主键
|
||||
if ($result) {
|
||||
if ($result && $getInsertId) {
|
||||
$insertId = self::db()->getLastInsID();
|
||||
if (is_string($this->pk) && $insertId) {
|
||||
$this->data[$this->pk] = $insertId;
|
||||
@@ -420,7 +422,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
public function saveAll($dataSet)
|
||||
{
|
||||
foreach ($dataSet as $data) {
|
||||
$result = $this->save($data);
|
||||
$result = $this->isUpdate(false)->save($data, [], false);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -858,10 +860,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
|
||||
/**
|
||||
* 初始化数据库对象
|
||||
* @access protected
|
||||
* @access public
|
||||
* @return \think\db\Driver
|
||||
*/
|
||||
protected static function db()
|
||||
public static function db()
|
||||
{
|
||||
$model = get_called_class();
|
||||
|
||||
|
||||
@@ -850,8 +850,8 @@ class Query
|
||||
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
||||
}
|
||||
// 预载入封装
|
||||
$table = $model::getTableName();
|
||||
$name = strtolower(basename(str_replace('\\', '/', $model)));
|
||||
$table = $info['model']::getTableName();
|
||||
$name = strtolower(basename(str_replace('\\', '/', $info['model'])));
|
||||
$this->via($name);
|
||||
$this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__');
|
||||
if ($closure) {
|
||||
|
||||
@@ -41,6 +41,7 @@ class Relation
|
||||
public function __construct($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;
|
||||
$localKey = $this->localKey;
|
||||
// 判断关联类型执行查询
|
||||
switch ($this->type) {
|
||||
case self::HAS_ONE:
|
||||
case self::BELONGS_TO:
|
||||
$result = $model::where($foreignKey, $this->parent->$localKey)->find();
|
||||
$result = $relation->where($foreignKey, $this->parent->$localKey)->find();
|
||||
if (false === $result) {
|
||||
$class = $this->model;
|
||||
$result = new $class;
|
||||
@@ -80,7 +81,7 @@ class Relation
|
||||
}
|
||||
break;
|
||||
case self::HAS_MANY:
|
||||
$result = $model::where($foreignKey, $this->parent->$localKey)->select();
|
||||
$result = $relation->where($foreignKey, $this->parent->$localKey)->select();
|
||||
break;
|
||||
case self::BELONGS_TO_MANY:
|
||||
// 关联查询
|
||||
@@ -147,7 +148,7 @@ class Relation
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -171,7 +172,7 @@ class Relation
|
||||
|
||||
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) {
|
||||
@@ -213,7 +214,7 @@ class Relation
|
||||
case self::HAS_ONE:
|
||||
case self::BELONGS_TO:
|
||||
// 模型关联组装
|
||||
$this->match($model, $relation, $result);
|
||||
$this->match($this->model, $relation, $result);
|
||||
break;
|
||||
case self::HAS_MANY:
|
||||
if (isset($result->$localKey)) {
|
||||
@@ -230,7 +231,7 @@ class Relation
|
||||
if (isset($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])) {
|
||||
@@ -287,7 +288,7 @@ class Relation
|
||||
{
|
||||
$foreignKey = $this->foreignKey;
|
||||
// 预载入关联查询 支持嵌套预载入
|
||||
$list = $model::where($where)->with($subRelation)->select();
|
||||
$list = $model->where($where)->with($subRelation)->select();
|
||||
|
||||
// 组装模型数据
|
||||
$data = [];
|
||||
@@ -348,7 +349,7 @@ class Relation
|
||||
$this->localKey = $localKey;
|
||||
|
||||
// 返回关联的模型对象
|
||||
return new $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -368,7 +369,7 @@ class Relation
|
||||
$this->localKey = $localKey;
|
||||
|
||||
// 返回关联的模型对象
|
||||
return new $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -388,7 +389,7 @@ class Relation
|
||||
$this->localKey = $localKey;
|
||||
|
||||
// 返回关联的模型对象
|
||||
return new $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +411,7 @@ class Relation
|
||||
$this->middle = $table;
|
||||
|
||||
// 返回关联的模型对象
|
||||
return new $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -434,4 +435,54 @@ class Relation
|
||||
->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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user