From 22b4fff3ddb391bcb5a65a38bc08f02848be55a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 7 Feb 2017 22:49:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BwithCount=E7=94=A8=E6=B3=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E7=BB=9F=E8=AE=A1=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index c9389356..ed846027 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1578,10 +1578,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($relation instanceof \Closure) { $closure = $relation; $relation = $key; + } elseif (is_string($key)) { + $name = $relation; + $relation = $key; } $relation = Loader::parseName($relation, 1, false); $count = $this->$relation()->relationCount($result, $closure); - $result->setAttr(Loader::parseName($relation) . '_count', $count); + if (!isset($name)) { + $name = Loader::parseName($relation) . '_count'; + } + $result->setAttr($name, $count); } }