mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
完善关联的save方法
This commit is contained in:
@@ -201,14 +201,26 @@ class HasMany extends Relation
|
|||||||
* @return Model|false
|
* @return Model|false
|
||||||
*/
|
*/
|
||||||
public function save($data)
|
public function save($data)
|
||||||
|
{
|
||||||
|
$model = $this->make($data);
|
||||||
|
return $model->save($data) ? $model : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建关联对象实例
|
||||||
|
* @param array $data
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
|
public function make($data = [])
|
||||||
{
|
{
|
||||||
if ($data instanceof Model) {
|
if ($data instanceof Model) {
|
||||||
$data = $data->getData();
|
$data = $data->getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存关联表数据
|
// 保存关联表数据
|
||||||
$model = new $this->model;
|
|
||||||
$data[$this->foreignKey] = $this->parent->{$this->localKey};
|
$data[$this->foreignKey] = $this->parent->{$this->localKey};
|
||||||
return $model->save($data) ? $model : false;
|
|
||||||
|
return new $this->model($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -244,17 +244,30 @@ class MorphMany extends Relation
|
|||||||
* @return Model|false
|
* @return Model|false
|
||||||
*/
|
*/
|
||||||
public function save($data)
|
public function save($data)
|
||||||
|
{
|
||||||
|
$model = $this->make($data);
|
||||||
|
|
||||||
|
return $model->save($data) ? $model : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建关联对象实例
|
||||||
|
* @param array $data
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
|
public function make($data = [])
|
||||||
{
|
{
|
||||||
if ($data instanceof Model) {
|
if ($data instanceof Model) {
|
||||||
$data = $data->getData();
|
$data = $data->getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存关联表数据
|
// 保存关联表数据
|
||||||
$pk = $this->parent->getPk();
|
$pk = $this->parent->getPk();
|
||||||
|
|
||||||
$model = new $this->model;
|
|
||||||
$data[$this->morphKey] = $this->parent->$pk;
|
$data[$this->morphKey] = $this->parent->$pk;
|
||||||
$data[$this->morphType] = $this->type;
|
$data[$this->morphType] = $this->type;
|
||||||
return $model->save($data) ? $model : false;
|
|
||||||
|
return new $this->model($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class MorphOne extends Relation
|
|||||||
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]);
|
||||||
}
|
}
|
||||||
$relationModel = $this->relation($subRelation)->find();
|
$relationModel = $this->relation($subRelation)->find();
|
||||||
|
|
||||||
@@ -179,7 +179,7 @@ class MorphOne extends Relation
|
|||||||
{
|
{
|
||||||
// 预载入关联查询 支持嵌套预载入
|
// 预载入关联查询 支持嵌套预载入
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [& $this]);
|
call_user_func_array($closure, [ & $this]);
|
||||||
}
|
}
|
||||||
$list = $this->query->where($where)->with($subRelation)->find();
|
$list = $this->query->where($where)->with($subRelation)->find();
|
||||||
$morphKey = $this->morphKey;
|
$morphKey = $this->morphKey;
|
||||||
@@ -206,7 +206,7 @@ class MorphOne extends Relation
|
|||||||
/**
|
/**
|
||||||
* 创建关联对象实例
|
* 创建关联对象实例
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return mixed
|
* @return Model
|
||||||
*/
|
*/
|
||||||
public function make($data = [])
|
public function make($data = [])
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user