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

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


Reply

12 Posts   1782 Views

Avatar
martimiz

20 October 2011 at 10:21pm Forum Moderator, 1091 Posts

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

22 October 2011 at 12:35am Community Member, 29 Posts

Thanks for the help guys. I got it working using [url=http://www.silverstripe.org/data-model-questions/show/18329#post308873]martimiz's code[/url]. 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

22 October 2011 at 12:47am Forum Moderator, 1796 Posts

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

22 October 2011 at 2:55am (Last edited: 22 October 2011 2:58am), Forum Moderator, 1091 Posts

@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 :-)