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

DataObject as has_many parent


Go to End


2 Posts   967 Views

Avatar
Loremiusz Ipsumowicz

Community Member, 1 Post

2 October 2014 at 12:20am

Hi.
Someone probably solved this before, but I failed at searching the forum for this.
Let's have a look at this piece of code:

class MyObject extends DataObject {
  private static $has_many = array(
    'Objects' => 'CommonObject',
  );
}


class MyPage extends Page {
  private static $has_many = array(
    'Objects' => 'CommonObject',
  );
}

class CommonObject extends DataObject {
  private static $has_one = array(
    'Parent' => 'DataObject',
  );
}

in simpler words, what I'm trying to achieve, is to have a DataObject that can act as a child in one-to-many relation, having a Page OR a DataObject as a Parent.
But declaring:

  private static $has_one = array(
    'Parent' => 'DataObject',
  );

gives me the following error:
DataList::create Can't find data classes (classes linked to tables) for DataObject
and it is true, as DataObject does not have a separate table in database, as it doesn't have any fields itself.

How do I achieve this?

Many thaks.

Avatar
camfindlay

Forum Moderator, 267 Posts

2 October 2014 at 2:32pm

Hey, this sounds like you are looking to do polymorphic relations... this is planned for version 3.2 (this is the doc on this, note it only applies to upcoming 3.2 version so is subject to changes http://doc.silverstripe.org/framework/en/trunk/topics/datamodel#has-one) of SilverStripe and in version 3.1 you are likely going to have to you many_many relations and appropriate CMS form components to deal with creating the and assigning CommonObjects to your MyObject and MyPage.