0

同じクラスに複数のmany_many関係を持つことが可能かどうか疑問に思っています。たとえば、テーブルA、テーブルB、およびテーブルCがあります。テーブルAとテーブルBの間にmany_many関係が必要であり、テーブルAとテーブルCの間に別のmany_many関係が必要です。これを試しましたが、IDが追加されていません。作成するテーブル。私はいくつかの検索を行って、このhttp://www.balbuss.com/multiple-many-many-s-of-the-same-class/を見つけましたが、それが今でも可能かどうかはわかりません。

どんな助けでも大歓迎です。

onAfterWriteを使用してリンクされたテーブルにデータを入力することを考えましたが、これを行う必要がありますか?

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'
);
}
4

1 に答える 1

1

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 ...');
于 2012-08-22T15:08:00.770 に答える