Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多对多模型中间表pivot提示外键不存在 #531

Open
iimeepo opened this issue May 6, 2024 · 0 comments
Open

多对多模型中间表pivot提示外键不存在 #531

iimeepo opened this issue May 6, 2024 · 0 comments

Comments

@iimeepo
Copy link

iimeepo commented May 6, 2024

vendor/topthink/think-orm/src/model/relation/BelongsToMany.php
方法eagerlyManyToMany拿到的中间表数据存放在了key为pivot的数组里,并且模型getData方法获取不到,这个就会导致后面的matchPivot方法报不存在中间表外键的错误,$list对应的查询sql打印出来是:SELECT yoshop_spec_value.*,pivot.id AS pivot__id,pivot.goods_id AS pivot__goods_id,pivot.spec_id AS pivot__spec_id,pivot.spec_value_id AS pivot__spec_value_id,pivot.wxapp_id AS pivot__wxapp_id,pivot.create_time AS pivot__create_time,pivot.update_time AS pivot__update_time FROM yoshop_spec_value INNER JOIN yoshop_goods_spec_rel pivot ON pivot.spec_value_id=yoshop_spec_value.spec_value_id WHERE pivot.goods_id = '670' ORDER BY id ASC

image image image

上一步的基础查询baseQuery方法里返回的是正常的,这里的$result返回的中间表数据是以pivot__开头的字段存放的,并且模型的getData方法可以拿到
image

尝试后发现,多对多关联的时候我加上排序导致的,如果去掉排序,这个结果就正常了
return $this->belongsToMany(SpecValue::class, GoodsSpecRel::class)
->order(['id' => 'asc']);

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

No branches or pull requests

1 participant