如何定义模型复合主键 #2957
huangdijia
started this conversation in
Show and tell
如何定义模型复合主键
#2957
Replies: 2 comments
-
很实用啊 |
Beta Was this translation helpful? Give feedback.
0 replies
-
跟model-cache有冲突,大家慎用。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
假设有这样一张表,设计的时候使用的不是自增主键
id
,而是用联合主键student_id+class_id
。现有需求,为 1001 每一科加 5 分(不要问为什么,产品的需求就是这么任性,哈哈),可是现有模型只支持单一主键,怎么办呢?
别慌,很简单,我们先定义一个 trait
使用也很简单:
那么好,现在就来实现需求吧:
事情还没完,需求虽然实现了,但是看控制台输出,有很多 notice
很显然,model-cache 不支持符合主键。
这里我给出我的解决方案,也是最简单的解决方案:针对这个 Model 移除
model-cache
功能。因为 project 在创建的时候自动生成的app/Model/Model.php
里是默认use Cacheable
了,那么只要 StudentScore 不要继承 app\Model\Model,直接继承\Hyperf\DbConnection\Model\Model
就好了。Beta Was this translation helpful? Give feedback.
All reactions