Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sharing a class with multiple DataModels

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

Page: 1
Go to End
Author Topic: 376 Views
  • kpower
    Avatar
    Community Member
    7 Posts

    Sharing a class with multiple DataModels Link to this post

    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?

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Sharing a class with multiple DataModels Link to this post

    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.

  • kpower
    Avatar
    Community Member
    7 Posts

    Re: Sharing a class with multiple DataModels Link to this post

    Smurkas,
    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

    376 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.