-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[RFC] Change return type of Model::findFirst or throw exception #14044
Comments
Agree. I believe the return false is somewhat reminiscent of legacy/older PHP functions that often return false. It's a bad practice. I would be in favor of actually moving everything to use exceptions and then have proper return types. I think we should use proper returns everywhere. Thus the return type would be Static analysis would improve so much further as well doing this. |
I guess that's even better idea. I will change title. The only problem with this is that changing it will require a lot of changes if someone already checks if findFirst returns false with I guess we can as well ignore it since it will be major version. Also what about What is good about exception that in most simple cases we could just catch it and return 404. |
I would argue that And I would say that we really should drop the ORM option with respect to Best practice is the developer should handle their exceptions as necessary. Catch and return custom JSON, arrays, custom view logic, or simply allow the error to bubble and be handled by default exception/error logic. It will automatically lead to better programming and cases of hidden bugs where people attempt to operate on a returned result of |
/cc @niden |
I agree that false value can be bad practice. But in our case it was quite useful. In caches for example. When attribute is null, you can tell entity was not queried. When false, you can discriminate between already queried but does not exist. |
I disagree on couple of points:
LINQ has
|
Forgetting the exceptions, I would be happy with just null for |
Well i agree that that's it not necessary that we throw any exception, this is just thing to consider. We can always add method findFirstOrDefault, or something like this, or even make it global configuration in Exceptions aren't serious signals from which you can recover, those are fatal errors, they are just information that something went wrong - in that case - you don't have any result, you can handle it easly. But having exception you can easly handle it on any level you want - in code where you exactly calls findFirst, or in controller action if you do for example many findFirst but if any of the records doesn't exist - you want to return 404 or even globally if you just want always to return something etc. For me null is the option we should consider at least - to make possible things like :?AnyClass, nice and easy. Though it won't be so clean because we can always have |
Making this change will of course break backwards compatibility. Since this is not a rewrite or changing of a pattern (say Active Record vs. Data Mapper), I think we should try and minimize the pain that a developer will have to go to refactor their code. Clearly the If we move to Exceptions, we will also have to consider them when using I am leaning towards:
|
There is also |
Resolved in #14089 |
Simple as that. It feels weird that
Phalcon\Mvc\Model::findFirst
returns object or false, why false exactly, i never undestood this really. Null makes more sense and also it works with nullable types in PHP 7 and above.I propose to add it to v4 while we still can.
The text was updated successfully, but these errors were encountered: