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.

Data Model Questions /

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

[SOLVED] One $has_many to TWO $has_one relationships. Awkward problem.


Go to End


12 Posts   6323 Views

Avatar
martimiz

Forum Moderator, 1391 Posts

20 October 2011 at 10:21pm

Hi all,
I'm probably totally wrong on this, I'm no architect, but I feel something odd in the concept of multiple has_many relationships to the same class. Something like a classroom having many chairs as well as many chairs.

If I wanted to split them, there must be a difference..? And if I would need, say, many benches as well as many chairs, why not subclass a SeatingContraption class into a Chair class and Bench class? I'd still have access to the base class as well...

I'd really like to see some simple example of where I would need to - just to get my head around this, because I'm kind of stuck in my own weird logic I guess :-(

Avatar
Sticks

Community Member, 31 Posts

22 October 2011 at 12:35am

Thanks for the help guys. I got it working using martimiz's code. I had a look at the docs after Willr pointed me in the right direction but I still didn't see how it worked exactly. Cheers for clearing it up martimiz!

Avatar
swaiba

Forum Moderator, 1899 Posts

22 October 2011 at 12:47am

It's great that it is working Sticks - so I apoloigize for hijacking this and continuing the disucssion!

I'd really like to see some simple example of where I would need to - just to get my head around this, because I'm kind of stuck in my own weird logic I guess

For me an example is Location and Voucher.

Relationship #1
A voucher can be shown (for sale) on many locations.

Relationship #2
A voucher can be redeemed at serveral at several locations.

I've done a hack to enable this... it's not pretty so I will not post and I avoid the use of the ORM for relationship #2.
I don't like changing the core (especially DataObject.php).

Avatar
martimiz

Forum Moderator, 1391 Posts

22 October 2011 at 2:55am

Edited: 22/10/2011 2:58am

@Sticks: glad to have been of help and same apologies here!

@swaiba: I see your point. For the sake of argument - if you were to approach it from the Location point of view, define a Seller and Redeemer that would do the actual work of selling and redeeming, both have_one Location. Vouchers will have their own set of sellers and redeemers - that may or may not share a Location. That feels sort of right to me, but again, I'm far from being an expert

But in terms of overhead and CMS management... I get it :-( So maybe dot notation is supposed to be a 'naming' shortcut for this kind of structure? Then indeed it should work for many_many as well.

Obviously I'd like to continue this discussion, but I fear this probably is not the right place :-)

Go to Top