Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Multiple has_many for one object can't be managed by CTM or DOM

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1207 Views
  • AdamJ
    Avatar
    Community Member
    145 Posts

    Multiple has_many for one object can't be managed by CTM or DOM Link to this post

    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.

  • AdamJ
    Avatar
    Community Member
    145 Posts

    Re: Multiple has_many for one object can't be managed by CTM or DOM Link to this post

    Anyone experienced this issue before?

  • meganub
    Avatar
    Community Member
    15 Posts

    Re: Multiple has_many for one object can't be managed by CTM or DOM Link to this post

    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

    1207 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.