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 /

Getting many_many relationships to sync correctly on complex data tables


Reply


6 Posts   1789 Views

Avatar
mark-e-mark

Community Member, 9 Posts

8 February 2010 at 2:51am

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.

Avatar
MDrollette

Community Member, 10 Posts

10 February 2010 at 4:57am

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

Avatar
Benedikt

Community Member, 16 Posts

10 February 2010 at 5:51am

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

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

Avatar
MDrollette

Community Member, 10 Posts

10 February 2010 at 6:11am

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.

Avatar
mark-e-mark

Community Member, 9 Posts

11 February 2010 at 9:46am

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.

Avatar
Ingo

Forum Moderator, 801 Posts

21 February 2010 at 7:23pm

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');
...