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

gen:model: Modifying Getter, Setting, and IDE code generation issues #6137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

guiqibusixin
Copy link

@guiqibusixin guiqibusixin commented Sep 6, 2023

ModelRewriteGetterSetterVisitor问题:
其中getter和setter是从当前model代码(AST)中获取的,没有考虑父类和trait等,重写方法可能会导致错误,例如
PHP Fatal error: Declaration of App\Model\User::setCreatedAt($created_at) must be compatible with Hyperf\Database\Model\Model::setCreatedAt($value): static in /project/hyperf-skeleton/app/Model/User.php on line 104
GenerateModelIDEVisitor问题:
Model中的方法是从最新model代码(AST)中获取的,是用其中方法名去反射获取方法可能会导致方法不存在的错误(model类已经在内存中加载,最最新的代码没有适用),例如一下命令·php bin/hyperf.php gen:model user --with-ide·,第一次ide会生成失败,再一次执行就可以成功了(第二次重新加载了代码,是最新的)

@guiqibusixin guiqibusixin changed the title gen:model: fix ModelRewriteGetterSetterVisitor and GenerateModelIDEVi… gen:model: Modifying Getter, Setting, and IDE code generation issues Sep 6, 2023
@limingxinleo
Copy link
Member

GenerateModelIDEVisitor 没看懂这个问题出在哪里

@guiqibusixin
Copy link
Author

GenerateModelIDEVisitor 没看懂这个问题出在哪里

某些visitor中使用了ReflectionClass把这个模型类加载到内存中去了,后面又有visitor(ModelRewriteGetterSetterVisitor等等)修改了语法树,如果有--with-ide,那么最后会执行GenerateModelIDEVisitor,里面使用的最新代码的语法树,并且使用了$reflection->getMethod来获取方法,新增的方法(例如ModelRewriteGetterSetterVisitor)会抛出方法不存在的异常,只有再一次执行命令 GenerateModelIDEVisitor 才会正常执行。
错误重现:添加 ModelRewriteGetterSetterVisitor,然后执行下面命令生成model, php bin/hyperf.php gen:model user --with-ide,其中user表包括address字段,所以生成了getAddress。
·Method App\Model\User::getAddress() does not exist
[ERROR] ReflectionException: Method App\Model\User::getAddress() does not exist in /project/hyperf/src/database/src/Commands/Ast/GenerateModelIDEVisitor.php:163·

@guiqibusixin
Copy link
Author

使用的 ModelRewriteGetterSetterVisitor 的目的,实体所有的属性由Getter和Setter来控制,方便管理(可以通过IDE查看哪些地方使用),如果直接使用getAttribute、类属性或数组等不方便查找,后面如果要加额外的操作也方便。参考doctrine或java中的hibernate等等

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.

None yet

2 participants