Skip to content

Commit

Permalink
改进关联自动删除
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 28, 2017
1 parent 4e23e89 commit d806b19
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,6 @@ protected function checkBeforeSave($data, $where)
}
}

// 关联写入检查
if ($this->together) {
$this->checkAutoRelationWrite();
}

// 数据自动完成
$this->autoCompleteData($this->auto);

Expand Down
31 changes: 24 additions & 7 deletions src/model/concern/RelationShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace think\model\concern;

use think\Collection;
use think\db\Query;
use think\Db;
use think\db\Query;
use think\Model;
use think\model\Relation;
use think\model\relation\BelongsTo;
Expand All @@ -30,14 +30,29 @@
*/
trait RelationShip
{
// 父关联模型对象
/**
* 父关联模型对象
* @var object
*/
private $parent;
// 关联模型

/**
* 模型关联数据
* @var array
*/
private $relation = [];
// 关联写入

/**
* 关联写入定义信息
* @var array
*/
private $together;
// 关联自动写入
private $relationWrite;

/**
* 关联自动写入信息
* @var array
*/
protected $relationWrite;

/**
* 设置父关联对象
Expand Down Expand Up @@ -115,6 +130,8 @@ public function together($relation)

$this->together = $relation;

$this->checkAutoRelationWrite();

return $this;
}

Expand Down Expand Up @@ -583,7 +600,7 @@ protected function autoRelationUpdate()
if ($val instanceof Model) {
$val->save();
} else {
$model = $this->getAttr($name);
$model = $this->getRelation($name);
if ($model instanceof Model) {
$model->save($val);
}
Expand Down

0 comments on commit d806b19

Please sign in to comment.