top-think / top-think/think-orm

中间表缺失表名

Open
#842 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

多对多关联的中间表,判断是否存在关联数据时报错:
SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''

<?php
namespace app\model;
use think\Model;
class Users extends Model
{
    //用户角色关联
    public function roles(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany('Role','user_role','role_id','user_id');
    }
}


$user = User::find(1);  
$user->roles()->attached(2); //有数据存在时报错

报错信息内容

[10501]SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''
[/htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php:867]
#0 /htdocs/api/vendor/topthink/think-orm/src/db/connector/Mysql.php(68): think\db\PDOConnection->getPDOStatement()
#1 /htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php(444): think\db\connector\Mysql->getFields()
#2 /htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php(401): think\db\PDOConnection->getTableFieldsInfo()
#3 /htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php(363): think\db\PDOConnection->getCachedSchemaInfo()
#4 /htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php(430): think\db\PDOConnection->getSchemaInfo()
#5 /htdocs/api/vendor/topthink/think-orm/src/db/PDOConnection.php(519): think\db\PDOConnection->getTableInfo()
#6 /htdocs/api/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php(60): think\db\PDOConnection->getFieldsType()
#7 /htdocs/api/vendor/topthink/think-orm/src/model/concern/DbConnect.php(87): think\db\Query->getFieldsType()
#8 /htdocs/api/vendor/topthink/think-orm/src/model/concern/Attribute.php(47): think\Model->getFields()
#9 /htdocs/api/vendor/topthink/think-orm/src/Model.php(150): think\Model->initializeData()
#10 /htdocs/api/vendor/topthink/think-orm/src/model/Pivot.php(49): think\Model->__construct()
#11 /htdocs/api/vendor/topthink/think-orm/src/Model.php(279): think\model\Pivot->__construct()
#12 /htdocs/api/vendor/topthink/think-orm/src/model/Pivot.php(75): think\Model->clone()
#13 /htdocs/api/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php(673): think\model\Pivot->newInstance()
#14 /htdocs/api/vendor/topthink/think-orm/src/db/BaseQuery.php(1575): think\db\BaseQuery->resultToModel()
#15 /htdocs/api/vendor/topthink/think-orm/src/model/relation/BelongsToMany.php(613): think\db\BaseQuery->find()
#16 /htdocs/api/app/controller/User.php(203): think\model\relation\BelongsToMany->attached()
#17 [internal function]: app\controller\User->setUserRole()
#18 /htdocs/api/vendor/topthink/think-container/src/Container.php(349): ReflectionMethod->invokeArgs()
#19 /htdocs/api/vendor/topthink/framework/src/think/route/Dispatch.php(190): think\Container->invokeReflectMethod()
#20 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(59): think\route\Dispatch->think\route\{closure}()
#21 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}()
#22 /htdocs/api/vendor/topthink/framework/src/think/route/Dispatch.php(164): think\Pipeline->then()
#23 /htdocs/api/vendor/topthink/framework/src/think/route/dispatch/Controller.php(112): think\route\Dispatch->responseWithMiddlewarePipeline()
#24 /htdocs/api/vendor/topthink/framework/src/think/route/Dispatch.php(60): think\route\dispatch\Controller->exec()
#25 /htdocs/api/vendor/topthink/framework/src/think/Route.php(717): think\route\Dispatch->run()
#26 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(59): think\Route->think\{closure}()
#27 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}()
#28 /htdocs/api/vendor/topthink/framework/src/think/Route.php(716): think\Pipeline->then()
#29 /htdocs/api/vendor/topthink/framework/src/think/Http.php(208): think\Route->dispatch()
#30 /htdocs/api/vendor/topthink/framework/src/think/Http.php(198): think\Http->dispatchToRoute()
#31 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(59): think\Http->think\{closure}()
#32 /htdocs/api/vendor/topthink/think-trace/src/TraceDebug.php(71): think\Pipeline->think\{closure}()
#33 [internal function]: think\trace\TraceDebug->handle()
#34 /htdocs/api/vendor/topthink/framework/src/think/Middleware.php(134): call_user_func()
#35 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(85): think\Middleware->think\{closure}()
#36 /htdocs/api/vendor/topthink/framework/src/think/Pipeline.php(66): think\Pipeline->think\{closure}()
#37 /htdocs/api/vendor/topthink/framework/src/think/Http.php(197): think\Pipeline->then()
#38 /htdocs/api/vendor/topthink/framework/src/think/Http.php(162): think\Http->runWithRequest()
#39 /htdocs/api/vendor/topthink/think-swoole/src/concerns/InteractsWithHttp.php(148): think\Http->run()
#40 /htdocs/api/vendor/topthink/think-swoole/src/concerns/InteractsWithHttp.php(127): think\swoole\Manager->handleRequest()
#41 /htdocs/api/vendor/topthink/think-container/src/Container.php(299): think\swoole\Manager->think\swoole\concerns\{closure}()
#42 /htdocs/api/vendor/topthink/think-container/src/Container.php(363): think\Container->invokeFunction()
#43 /htdocs/api/vendor/topthink/think-swoole/src/Sandbox.php(81): think\Container->invoke()
#44 /htdocs/api/vendor/topthink/think-swoole/src/concerns/WithApplication.php(88): think\swoole\Sandbox->run()
#45 /htdocs/api/vendor/topthink/think-swoole/src/concerns/InteractsWithHttp.php(120): think\swoole\Manager->runInSandbox()
#46 [internal function]: think\swoole\Manager->think\swoole\concerns\{closure}()
#47 {main}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/model/relation/BelongsToMany.php around attached(), then follow the pivot creation through src/model/Pivot.php and the schema lookup in src/db/PDOConnection.php. Reproduce the provided relation with MySQL and attached(2); done means it no longer queries an empty table name and correctly reports the association.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.