Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Getting many_many relationships to sync correctly on complex data tables

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

Page: 1
Go to End
Author Topic: 1643 Views
  • mark-e-mark
    Avatar
    Community Member
    9 Posts

    Getting many_many relationships to sync correctly on complex data tables Link to this post

    Hi Guys/Girls

    Let me start by saying that I'm new to SilverStripe but loving it! I have a background in PHP, CodeIgniter, and Drupal. Let's just say that Drupal is going bye-bye

    That being said, I am having issues getting many_many relationships to sync correctly on ManyManyComplexTableField tables. I'm sure it is something simple - but I've searched and scoured and tried various alterations - all to no avail (eg. setParentClass, using belong_many_many, even trying to specify the join on the ManyManyComplexTableField, etc).

    I have two DataObjects that I wish to be able to manipulate in the CMS backend: 'Feature' and 'PlanType'. Each PlanType can have multiple Features, and each Feature may be applicable to multiple plans. I can get the options for PlanType to show up on Feature and v.v.

    However:
    - If I add a PlanType and check that it has a certain Feature on the ManyManyComplexTableField it will record and save the value without issue
    - If I then go to Feature and look at the ManyManyComplexTableField, it does NOT show that PlanType is linked

    I have figured out that this is because the database is creating two tables instead of using the one table (ie. it is creating Feature_PlanTypes AND PlanType_Feature). Each side of the relationship is using separate tables rather that the one.

    How (in dear God's name) do I get both sides of the many_many relationship to use the same table rather (and thus syncing) rather than using the two separate tables generated by SS.

    I'm sure it is simple.

    Please forgive me if this does not make sense - I'm frustrated and writing late at night.

    For your reference I have included the 'offending' PHP files:

    MyFeatureAdmin.php -> CMS admin

    <?php
    class MyFeatureAdmin extends ModelAdmin {
    public static $managed_models = array (
    'Feature',
    'PlanType'
    );

    static $url_segment = 'features';
    static $menu_title = 'Features';
    }
    ?>

    Feature.php -> Feature DataObject

    <?php
    class Feature extends DataObject {

    static $db = array('Feature' => 'Varchar(100)');
    static $many_many = array('PlanTypes' => 'PlanType'); //link to PlanType DataModel
    static $summary_fields = array('Feature');

    function getCMSFields(){
    $fields = parent::getCMSFields();
    $tablefield = new ManyManyComplexTableField($this, 'PlanTypes', 'PlanType', array('Type'=>'Type'), 'getCMSFields');
    $fields->addFieldToTab('Root.PlanTypes', $tablefield);

    return $fields;
    }
    }
    ?>

    PlanType.php -> PlanType DataObject

    <?php
    class PlanType extends DataObject {

    static $db = array('Type' => 'Varchar(100)');
    static $many_many = array('Features' => 'Feature');
    static $summary_fields = array( 'Type');

    function getCMSFields(){
    $fields = parent::getCMSFields();
    $tablefield = new ManyManyComplexTableField($this, 'Features', 'Feature', array('Feature'=>'Feature'), 'getCMSFields');
    $fields->addFieldToTab('Root.Features', $tablefield);

    return $fields;
    }
    }
    ?>

    If I could just get this sorted - SS would be the be-all in my world.

    Thanks for any input.

  • MDrollette
    Avatar
    Community Member
    10 Posts

    Re: Getting many_many relationships to sync correctly on complex data tables Link to this post

    I've been searching for the exact same thing for the last day or so. I can't seem to find any solution... only a reference to this ticket that may be a culprit? http://open.silverstripe.org/ticket/4546

  • Benedikt
    Avatar
    Community Member
    16 Posts

    Re: Getting many_many relationships to sync correctly on complex data tables Link to this post

    static $many_many
    Shouldn't one side of the relationship be $belongs_many_many ?

    Cf. also this bug: http://open.silverstripe.org/ticket/4546

  • MDrollette
    Avatar
    Community Member
    10 Posts

    Re: Getting many_many relationships to sync correctly on complex data tables Link to this post

    I've tried with one class as $belongs_many_many but it doesn't seem to have any effect. I still can't manage the relation from the $belongs_many_many side.

  • mark-e-mark
    Avatar
    Community Member
    9 Posts

    Re: Getting many_many relationships to sync correctly on complex data tables Link to this post

    Thanks for the input guys. Nice to know that I wasn't the only one struggling with this issue.

    Yeah .. I've tried the belongs_many_many on one side too .. still no luck.

    Will keep my fingers crossed that this bugfix may solve the problem.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Getting many_many relationships to sync correctly on complex data tables Link to this post

    There's a bug slated for 2.4 which might affect this: http://open.silverstripe.org/ticket/4706

    > I have figured out that this is because the database is creating two tables instead of using the one table (ie. it is creating Feature_PlanTypes AND PlanType_Feature). Each side of the relationship is using separate tables rather that the one.

    That will definetly be solved by using $belongs_many_many on one of the two datamodels. Keep in mind, the "old" faulty table won't be deleted automatically.

    class Feature extends DataObject {
    static $belongs_many_many = array('PlanTypes' => 'PlanType');
    ...

    class PlanType extends DataObject {
    static $many_many = array('Features' => 'Feature');
    ...

    1643 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.