-
Notifications
You must be signed in to change notification settings - Fork 821
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
Naming DBField as "Data" confuses core #8088
Comments
I like the idea of throwing an Exception dev/build if a DataObject uses a reserve keyword as a field name. If we're going to do this, it probably be worthwhile to identify further reserve keywords that might casue issues. |
Sure, or just add a single one to the list for now given the frameworks' 10+ year history without any similar issue (AFAIK) otherwise we might be waiting a while ;-) |
Suggested code-snippet from @phptek to help out the academy students:
|
I think adding a new exception might constitute a major change in semver. If you're happy to make the PR against the master branch for SS5 then that sounds OK to me! |
@robbieaverill interesting. Is throwing an exception in any area of the dev/build pipeline considered a major change? What we could do instead is simply echo a warning to the CLI / GUI during dev/build. but not explicitly halt execution. Then again, dev's may quite easily miss that - but is that better than the status quo? |
We’ve “avoided” this issue in the past by issuing a |
I thought I read somewhere that SilverStripe (devs) are really looking to move away from And yeah, you're bang-on wr/t this being a change to something that is already "broken". Nice one. I'll leave it to the academy devs then :-) |
@robbieaverill we tried targeting master but it included a bunch of unrelated commits. We've targeted the |
Another one to add to the list: Edit: because of |
Couldn't the exception be whenever the dbfield matches a method on that class? E.g.
This still allows db fields to be overridden with In 5.x obviously due to the breaking change. :) |
I like it. |
Note that there's a PR already at #8731 |
Closing as a duplicate of #6329 |
Affected Version
framework 4.1.0 / PHP7.0
Description
Creatiing a simple DataObject with a single
Text
field and naming it "Data" will cause problems when Calling$this->obj('Data')
in any context. In my case, I am using the restfulserver package which attempts to decompose aDataObject
into JSON viaJSONDataFormatter
which makes use ofViewableData::obj()
when casting.This occurs becuase of the core method
DataObject::data()
which is "blindly" called byViewableData::obj()
instead of the desiredDBField
bySilverStripe\Core\CustomMethods::hasMethod()
.The problem can be demo'd fairly succinclty below:
Steps to Reproduce
Workaround
The workaround is to declare a method on your datamodel named for the field and call
getField('Data')
from it, thus inverting otherwise default control.Suggestions
"Data" is used in a DataObject context as
DataObject::data()
and is therefore a reserved word. As such, userland logic could receive anException
ondev/build
when attempting to declare a field-name that is reserved. The reserved field-names could be declared as an immutible array inDataObject
to permit further fields to be added by core-devs in a flexible manner going forward.Related PRs
The text was updated successfully, but these errors were encountered: