From e5de0d87dd4fc876e509a6f4bd8dc33ed2a85ea7 Mon Sep 17 00:00:00 2001 From: jwj Date: Mon, 28 Apr 2025 18:22:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=AD=E9=97=B4=E8=A1=A8?= =?UTF-8?q?=E7=BC=BA=E5=A4=B1=E8=A1=A8=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' ``` ```php true, ]; } /** * 用户 * @return \think\model\relation\BelongsTo */ public function user(): \think\model\relation\BelongsTo { return $this->belongsTo(User::class, 'user_id'); } /** * 类型 * @return \think\model\relation\BelongsTo */ public function type(): \think\model\relation\BelongsTo { return $this->belongsTo(MessageType::class, 'type_id'); } } ``` ```php \app\model\MessageTypeUser::where('merchant_id', 0)->select() ``` --- src/model/Pivot.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/model/Pivot.php b/src/model/Pivot.php index 44cf2243..c77903c5 100644 --- a/src/model/Pivot.php +++ b/src/model/Pivot.php @@ -13,6 +13,7 @@ namespace think\model; +use think\helper\Str; use think\Model; /** @@ -26,6 +27,11 @@ class Pivot extends Model * @var Model */ public $parent; + + /** + * 中间表名称. + * @var string + */ protected $pivotName; /** @@ -56,8 +62,8 @@ public function __construct(array $data = [], ?Model $parent = null, string $tab */ protected function init() { - if (is_null($this->getOption('name'))) { - $this->setOption('name', $this->pivotName); + if (null === $this->getOption('name')) { + $this->setOption('name', $this->pivotName ?: Str::snake(class_basename(static::class))); } }