1

我正在尝试创建这样的团队列表。

TeamHolder - 适用于所有团队。有 Teampages 作为孩子

TeamPage - 团队的描述和形象。还有一个与 TeamMember(Dataobject) 的 has_one 关系

TeamMember - 拥有成员。在这里,您还应该能够定义玩家所在的团队。这是我的代码。但我不知道如何与我的球队页面建立 has_many 关系,以便您可以为一名球员选择多个球队。有没有人解决这个问题?

http://www.sspaste.com/paste/show/526422c0d33bc

提前谢谢

4

1 回答 1

3

如果您希望 1 名玩家 (TeamMember) 在多个团队中,并且团队中有多个玩家,您需要一个 many_many。如果在这里使用 has_many,那么 Team 将有一个 PlayerID,这意味着每个团队中只能有 1 个 Player。

class TeamPage extends Page {
    private static $many_many = array('TeamMembers' => 'TeamMember');
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root', Tab::create('MembersTab', 'Team Members'));
        $fields->addFieldToTab('Root.MembersTab', GridField::create('TeamMembers', 'The Team Members', $this->TeamMembers(), GridFieldConfig_RelationEditor::create());
        return $fields;
    }
}
class TeamMember extends DataObject {
    private static $belongs_many_many = array('Teams' => 'TeamPage');
    public function getCMSFields() {
        $fields = FieldList::create();
        if (!$this->isInDB()) {
            // if the TeamMember has not been saved yet, display a message that it needs to be saved before teams can be assigned
            $fields->push(ReadOnlyField::create('Teams', '', 'Save to assign Teams'));
        } else {
            $config = GridFieldConfig_RelationEditor::create();
            // if you don't want the "add a new team" button on this grid, you can remove that with the following line:
            // $config->removeComponentsByType('GridFieldAddNewButton');
            $fields->push(GridField::create('Teams', 'Team this Member is in', $this->Teams(), $config);
        }
        return $fields;
    }
}

请注意,我使用GridFieldConfig_RelationEditor的不是GridFieldConfig_RecordEditor它,而是向您的网格字段添加一个GridFieldAddExistingAutocompleter允许您链接对象的。

于 2013-10-21T02:01:14.913 回答