From 7983db9587b1686a6fbe30edc7875f580aba0495 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 30 Nov 2016 22:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E6=80=81=E4=B8=80=E5=AF=B9=E5=A4=9A?= =?UTF-8?q?=E5=85=B3=E8=81=94=E6=96=B9=E6=B3=95=E5=8F=82=E6=95=B0=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 0f28b70a..32ce4e52 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1367,31 +1367,39 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * MORPH MANY 关联定义 * @access public * @param string $model 模型名 - * @param string $id 关联外键 - * @param string $morphType 多态字段名 + * @param string|array $morph 多态字段信息 * @param string $type 多态类型 * @return Relation */ - public function morphMany($model, $id, $morphType = '', $type = '') + public function morphMany($model, $morph, $type = '') { // 记录当前关联信息 - $model = $this->parseModel($model); - $type = $type ?: Loader::parseName($this->name); - $name = Loader::parseName(basename(str_replace('\\', '/', $model))); - $morphType = $morphType ?: Loader::parseName($name) . '_type'; - return $this->relation()->morphMany($model, $id, $morphType, $type); + $model = $this->parseModel($model); + $type = $type ?: Loader::parseName($this->name); + if (is_array($morph)) { + list($foreignKey, $morphType) = $morph; + } else { + $morphType = $morph . '_type'; + $foreignKey = $morph . '_id'; + } + return $this->relation()->morphMany($model, $foreignKey, $morphType, $type); } /** * MORPH TO 关联定义 * @access public - * @param string $morphType 多态字段名 - * @param string $foreignKey 外键名 + * @param string|array $morph 多态字段信息 * @return Relation */ - public function morphTo($morphType, $foreignKey) + public function morphTo($morph) { // 记录当前关联信息 + if (is_array($morph)) { + list($foreignKey, $morphType) = $morph; + } else { + $morphType = $morph . '_type'; + $foreignKey = $morph . '_id'; + } return $this->relation()->morphTo($morphType, $foreignKey); }