Skip to content

Conversation

big-dream
Copy link
Contributor

错误信息:

SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''

模型:

<?php
namespace app\model;

/**
 * 消息类型接收人
 */
class MessageTypeUser extends \think\model\Pivot
{
    // 模型参数
    protected function getOptions(): array
    {
        return [
            'autoWriteTimestamp' => 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');
    }
}

查询:

\app\model\MessageTypeUser::where('merchant_id', 0)->select()

```
SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''
```

```php
<?php
namespace app\model;

/**
 * 消息类型接收人
 */
class MessageTypeUser extends \think\model\Pivot
{
    // 模型参数
    protected function getOptions(): array
    {
        return [
            'autoWriteTimestamp' => 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()
```
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.23%. Comparing base (09c39f5) to head (e5de0d8).

Additional details and impacted files
@@            Coverage Diff            @@
##                4.0     #759   +/-   ##
=========================================
  Coverage     46.23%   46.23%           
- Complexity     3194     3195    +1     
=========================================
  Files            72       72           
  Lines          7663     7663           
=========================================
  Hits           3543     3543           
  Misses         4120     4120           
Flag Coverage Δ
unittests 46.23% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@liu21st
Copy link
Member

liu21st commented Apr 28, 2025

你直接查询中间表数据?通常来说中间表对象是通过多对多关联的newPivot方法创建的
如果你需要直接调用中间表模型的话 建议直接设置pivotName或name属性即可

@big-dream
Copy link
Contributor Author

是的 在3.0版本也能正常查询出来的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants