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 for one object can't be managed by CTM or DOM


Go to End


3 Posts   2256 Views

Avatar
AdamJ

Community Member, 145 Posts

1 June 2011 at 11:22pm

Edited: 01/06/2011 11:22pm

So I have a situation where a single class has 3 has_many relationships to a dataobject. I've got it setup as follows:

class DistributorsPage extends Page {
  public static $has_many = array(
    'Restaurants' => 'Distributor.Restaurants',
    'Retails' => 'Distributor.Retails',
    'Wholesales' => 'Distributor.Wholesales'
  );
}
class Distributor extends DataObject {
  static $has_one = array(
    'Restaurants' => 'DistributorsPage',
    'Retails' => 'DistributorsPage',
    'Wholesales' => 'DistributorsPage'
  );
}

Which seems to have worked fine, and has created the correct database columns of WholesalesID, RetailsID, RestaurantsID on the Distributor table.

The issue I'm having though is that for the three DOM tables (and the same for CTM tables) all display the same results, which seems to be whichever relation I have last in the has_one declaration. So if I move the 'Restaurants' relation to last, the results for Restaurants will be displayed on all three relation tables in the admin.

This is my DOM code:

$restaurantManager = new DataObjectManager(
  $this, 
  'Restaurants', 
  'Distributor',
  array(
    'Name' => 'Name',
    'State' => 'State'
  ),
  'getCMSFields_forPopup'
);
$fields->addFieldToTab( 'Root.Content.Restaurant', $restaurantManager);

Anyone see anything I'm doing wrong, or is this a bug in the core? As an aside, setting the source class to "Distributor.Restaurants" causes a bad singleton error for a class that doesn't exist.

Avatar
AdamJ

Community Member, 145 Posts

6 June 2011 at 7:03pm

Anyone experienced this issue before?

Avatar
meganub

Community Member, 15 Posts

7 September 2011 at 10:09pm

Hi Adam,

I just experienced this same issue using CTM (has_many).

I poked around, and it looks like a core bug in that the CTM is not getting the right joinField set. I.e. For your example, you should have WholesalesID, RetailsID and RestaurantsID in your respective CTM's, but it appears to be just grabbing the first relationship it can find. THe culprit is line 55 of HasManyComplexTableField.

The fix is simple though, after you create your CTM just add a line setting the joinField (it's a public property) directly. I.e.

$tablefield = new HasManyComplexTableField(
			$this,
			$getter,
			$type,
			$fields,
			'getCMSFields_forPopup'
		);

$tablefield->joinField = 'RetailsID';

This should fix it.

Cheers

Robbie