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.

Data Model Questions /

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

Sharing a class with multiple DataModels

Go to End

3 Posts   669 Views


Community Member, 7 Posts

11 September 2011 at 10:25pm

Suppose that I have two DataModels called Seller and Buyer. I have another reusable class called, say, ProfileImage. As I understand, the ProfileImage class must be renamed as Seller_ProfileImage, if I need to access that class from the Seller class. Similarly, if I need to access that class from the Buyer class, it should be named as Buyer_ProfileImage. That's the way I heard that the class autoloader in SilverStripes works.

If ProfileImage contains only some generic logic which can be reused by both Buyer and Seller, is there a way to make it accessible from both Seller and Buyer classes, without having to use explicit require() and include() calls?


Community Member, 288 Posts

11 September 2011 at 11:35pm

Edited: 11/09/2011 11:35pm

Well without knowing too much about how you want to use the ProfileImage with the Buyer and Seller it does sound like a has_one relationship.
So something like

static $has_one = array('ProfileImage' => 'ProfileImage');

In the Buyer and Seller class and they can now have one ProfileImage associated to them. If that's not what you're looking for then you need to use a Decorator to share functionality. Also you never use explicit require() and include() calls in SilverStripe. All the code you put in your code folder is always accessible throughout your application so you can always write $image = new ProfileImage();

I would also really recommend going through the tutorials here if you have not done so already.


Community Member, 7 Posts

12 September 2011 at 12:13am

Sorry for not being able to provide much details. And yes, ProfileImage is a DataObject implementer that I designed to use with has_one relation in the way you mentioned. Frankly I've misinterpreted some instruction written in a book, which says that there should be a filename_ prefix for the ProfileImage class ONLY if it is created in the same file. My bad :)