Silverstripe 3.1 - 一个球员有很多球队(Silverstripe 3.1 - O

2019-10-18 19:15发布

我想创建这样一个团队名单。

TeamHolder - 所有球队。 有Teampages儿童

TeamPage - 团队的描述和形象。 而且还有一个HAS_ONE关系TeamMember(数据对象)

TeamMember - 拥有成员。 在这里你也应该可以定义哪些队玩家。 这里是我的代码这一点。 但是我不知道我可以做的has_many关系到我的团队网页,让您可以选择多个团队的一名球员。 有没有人一个解决方案?

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

THX提前

Answer 1:

如果你想1播放器(TeamMember)是在多个团队和团队有多个玩家,你需要一个many_many。 如果您在此处使用的has_many,那么球队将有一个PlayerID,这意味着只能在每队1名球员。

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您gridfield,让你链接的对象。



文章来源: Silverstripe 3.1 - One player has many teams