Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Abstract DataObjects


Go to End


5 Posts   1764 Views

Avatar
willmorgan

Community Member, 11 Posts

13 February 2013 at 11:44pm

Edited: 14/02/2013 12:16am

Hey guys. I want to create an abstract DataObject but as many know, the database builder (and likely other things) don't like this.

I think this would be a pretty useful feature to have, as without it, we have to take some weird steps around the lack of support.

What is the lead dev consensus on this? Is it worth working on, and if so, what version would you want it in?

Avatar
Willr

Forum Moderator, 5523 Posts

14 February 2013 at 9:10pm

what version would you want it in?

This would be a change for 3.2 as 3.1 is in testing nearing release so probably too late to get something like this in as I'm not sure what would break. As far as I know, you cannot have an abstract class extend a non abstract class so you'd need to look at making the whole DO chain abstract (viewable data, I think Object already is). To me it makes semantic sense for it to be abstract (you don't create a DataObject, in that case you'd use ArrayData).

Avatar
annap

Community Member, 2 Posts

10 August 2014 at 10:55am

Hi,

some of my classes, that extend DataObject (e.g. Product) have methods with exact the same content.
I want to outsource these methods in a parent class: MyDataObject.
My Product class should extend MyDataObject instead of extending DataObject directly,
but I'm not able to do that, I get the same Problem as described above. And I don't want MyDataObject to have a database table. Declaring MyDataObject abstract didn't help.

Is there another way to achieve this:
avoiding duplicate code
?

Thanks,
Anna

Avatar
Willr

Forum Moderator, 5523 Posts

10 August 2014 at 1:31pm

@annap, in that case you want to look at DataExtensions (which can be added to your data object without adding a table) or look at PHP Traits (http://addons.silverstripe.org/add-ons/simonwelsh/trait-loader)

Avatar
annap

Community Member, 2 Posts

11 August 2014 at 8:44am

Yes, that helped me. Thank you. DataExtension works for me.