From 62aa6a0f142b70febe0e16a12d6da8ce5b61f170 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 28 Mar 2017 11:56:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BBelongsTo=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 4 ++- library/think/model/relation/BelongsTo.php | 34 +++++++++++++++++++++- library/think/model/relation/MorphTo.php | 2 +- library/think/model/relation/OneToOne.php | 2 ++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 40b8dfe1..8aade6c8 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1651,7 +1651,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->parseModel($model); $foreignKey = $foreignKey ?: $this->getForeignKey($model); $localKey = $localKey ?: (new $model)->getPk(); - return new BelongsTo($this, $model, $foreignKey, $localKey, $joinType); + $trace = debug_backtrace(false, 2); + $relation = Loader::parseName($trace[1]['function']); + return new BelongsTo($this, $model, $foreignKey, $localKey, $joinType, $relation); } /** diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 6996526d..327939d8 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -25,7 +25,7 @@ class BelongsTo extends OneToOne * @param string $localKey 关联主键 * @param string $joinType JOIN类型 */ - public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER') + public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER', $relation = null) { $this->parent = $parent; $this->model = $model; @@ -33,6 +33,7 @@ class BelongsTo extends OneToOne $this->localKey = $localKey; $this->joinType = $joinType; $this->query = (new $model)->db(); + $this->relation = $realtion; } /** @@ -168,4 +169,35 @@ class BelongsTo extends OneToOne $result->setAttr(Loader::parseName($relation), $relationModel); } + /** + * 添加关联数据 + * @access public + * @param Model $model 关联模型对象 + * @return Model + */ + public function associate($model) + { + $foreignKey = $this->foreignKey; + $pk = $model->getPk(); + + $this->parent->setAttr($foreignKey, $model->$pk); + $this->parent->save(); + + return $this->parent->setAttr($this->relation, $model); + } + + /** + * 注销关联数据 + * @access public + * @return Model + */ + public function dissociate() + { + $foreignKey = $this->foreignKey; + + $this->parent->setAttr($foreignKey, null); + $this->parent->save(); + + return $this->parent->setAttr($this->relation, null); + } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index c660a757..9c97500c 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -34,7 +34,7 @@ class MorphTo extends Relation * @param array $alias 多态别名定义 * @param string $relation 关联名 */ - public function __construct(Model $parent, $morphType, $morphKey, $alias = [], $relation) + public function __construct(Model $parent, $morphType, $morphKey, $alias = [], $relation = null) { $this->parent = $parent; $this->morphType = $morphType; diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index e2b37421..b0cf931d 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -30,6 +30,8 @@ abstract class OneToOne extends Relation protected $joinType; // 要绑定的属性 protected $bindAttr = []; + // 关联方法名 + protected $relation; /** * 设置join类型