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

Multiple has_many relations with the same class


Go to End


5 Posts   2541 Views

Avatar
EelkeSpaak

Community Member, 4 Posts

19 February 2010 at 12:00am

I am trying to set up the following relation, but I am doubtful as to whether it is going to work:

class AvailabilityPage extends SiteTree {
	static $has_many = array(
		"HighSeasonIntervals" => "DateInterval",
		"FullyBookedIntervals" => "DateInterval"
	);
}

class DateInterval extends DataObject {
	static $db = array(
		"StartDate" => "Date",
		"EndDate" => "Date"
	);
	
	static $has_one = array(
		"AvailabilityPage" => "AvailabilityPage"
	);
}

The resulting database schema does not seem to distinguish between a DateInterval belonging either to the HighSeasonIntervals set on AvailabilityPage, or to the FullyBookedIntervals set. Does anyone know whether or not this is going to work?

If (as I suspect), it is not going to work directly, could anyone give me some pointers on how to implement such a relation? There is nothing on this topic in the Silverstripe documentation, unfortunately.

Many thanks in advance!

Avatar
EelkeSpaak

Community Member, 4 Posts

19 February 2010 at 12:10am

Now that I have thought about it some more, the impossibility of the relation I described above seems like a fundamental shortcoming of relational data models in general. Since any SQL database is a relational data model, it's never going to work :)

I have worked around the problem in the following (OO-wise ugly) way:

class AvailabilityPage extends Page {
	static $has_many = array(
		"HighSeasonIntervals" => "DateInterval_1",
		"FullyBookedIntervals" => "DateInterval_2"
	);
}

class DateInterval extends DataObject {
	static $db = array(
		"StartDate" => "Date",
		"EndDate" => "Date"
	);
}

class DateInterval_1 extends DateInterval {
	static $has_one = array(
		"AvailabilityPage" => "AvailabilityPage"
	);
}

class DateInterval_2 extends DateInterval {
	static $has_one = array(
		"AvailabilityPage" => "AvailabilityPage"
	);
}

Still, of course, if anyone has a better idea, I'd be glad to hear it. This might actually be quite an interesting general issue in object/relational mapping.

Avatar
ajshort

Community Member, 244 Posts

19 February 2010 at 12:29am

Actually this issue was addressed in 2.4! See http://open.silverstripe.org/ticket/4632 for more information.

Avatar
EelkeSpaak

Community Member, 4 Posts

19 February 2010 at 12:32am

Great! Thanks for pointing that out; I'll stick with my workaround this time (since I'm using 2.3.6), but I'll keep it in mind for future reference :)

Avatar
JimAasheim

Community Member, 12 Posts

22 February 2010 at 6:58am

Hi,

in a relational database you'd use a m:n relation. But that's indeed a mess to implement...