0
votes

I'm just wondering if it is possible to have multiple many_many relationships on the same class. For example, I have a Table A, Table B and Table C. I want a many_many relationship between Table A and Table B and another many_many relationship between Table A and Table C. I have tried this but it is not adding the IDs to the tables it creates. I did some searching and found this http://www.balbuss.com/multiple-many-many-s-of-the-same-class/ and am not sure whether it is even possible now.

Any help is very much appreciated.

I had a thought about using onAfterWrite to populate the linked table but should I need to do this?

class Table B extends DateObject {
static $belongs_many_many = array (
    'TableAs' => 'TableA'
);
}

class Table C extends DateObject {
static $belongs_many_many = array (
    'TableAs' => 'TableA'
);
}

class Table A extends Page {
    static $many_many = array (
    'TableB' => 'TableB',
    'TableC' => 'TableC'
);
}
1
no, onAfterWrite doesn't sound like the right way to fix this. could you add your current code please? - Zauberfisch
Thanks for the response on this @Zauberfisch. I've added the code above which is the bit that relates to the many_many relationship. - MillyMonster
the relations are certainly possible this way, I my self have many cases where my relations look just like yours. So you are saying relations are not saved? Could you also post the code of how you save the relations? what are you using to manage the relations? - Zauberfisch
Ah that is where my confusion arises. I am fairly new to this area of SilverStripe but should this not work similarly to the has_one relationship? I assumed (probably naively) that the IDs would be automatically added to the tables once I create a new Table B for example. Is this not the case? - MillyMonster
well, yes relations are auto saved, but only if you use the right tool for it. For example a ManyManyComplexTableField or a ManyManyDataObjectManager in ss2 or the GridField in ss3 would save the relation - Zauberfisch

1 Answers

1
votes

for managing many_many relations in SilverStripe you can use to following field:

ss2.4: ManyManyComplexTableFieldor ManyManyDataObjectManager, both provide a list of all records of that type, and let you choose the relation with checkboxes

ss3.0: GridField, but you might have to add the checkboxes your self, not sure

also, in both 2.4 and 3.0 there are some dropdown fields and other field that support many_many relations as well (for example the TreeMultiselectField)


I am not sure what you mean with "iterate through each instance of TableA" in the comment, if you just want to loop all TableAs that are asigned to a single record of TableB then you can do:

foreach ($tableBrecord->TableAs($filter = "", $sort = "", $join = "", $limit = "") as $tableArecord) {
   // do something with $tableArecord
}

But if you really want to do something on ALL records of TableA then you can do:

foreach(DataObject::get('TableA', $filter = "", $sort = "", $join = "", $limit = "") as $tableArecord) {
   // do something with $tableArecord
}

if you have a LOT of TableA records this might be a little slow, so you could also do a native query like so

 DB::query('UPDATE TableA SET ...');