3093 Posts in 875 Topics by 654 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1487 Views |
-
Getting many_many relationships to sync correctly on complex data tables

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 linkedI 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.
-
Re: Getting many_many relationships to sync correctly on complex data tables

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
-
Re: Getting many_many relationships to sync correctly on complex data tables

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
-
Re: Getting many_many relationships to sync correctly on complex data tables

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.
-
Re: Getting many_many relationships to sync correctly on complex data tables

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.
-
Re: Getting many_many relationships to sync correctly on complex data tables

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');
...
| 1487 Views | ||
|
Page:
1
|
Go to Top |




