【问题标题】:Silverstripe $many_many relationship with an attribute on the relationshipSilverstripe $many_many 关系与关系上的一个属性
【发布时间】:2013-07-29 23:56:15
【问题描述】:

添加 many_many 关系时,很像 silverstripe 指南中的项目与导师关系:

http://doc.silverstripe.org/framework/en/tutorials/5-dataobject-relationship-management

我想根据关系记录一个属性。因此,例如“活动” - 项目导师的是/否字段。但是,对于与她相关的不同项目,导师可能对活跃有不同的价值。

使用 Silverstripe 的内置工具实现这一目标的最佳方法是什么?

更新 在 IRC 的帮助下和下面的答案。我离得更近了一点,它不起作用。我发现了这个: https://github.com/chillu/silverstripe-framework/blob/c8136f5d4c8a37a4da274cd1c93907c0a2af86a7/docs/en/reference/grid-field.md 这似乎非常相关。

因此,DebatePages 有 many_many 小组成员,他们可以对每场辩论进行不同的投票。

辩论页面.php

  private static $many_many = array(
    'Panelists'     => 'Panelist',
    'RelationTags'  => 'Tag'
  );
  public static $many_many_extraFields = array(
    'Panelists' => array('Motion' => 'Boolean')
  );




public function getCMSFields() {
    .....
    if($this->ID) {
            $panelistFields = singleton('Panelist')->getCMSFields();
            $panelistFields->addFieldToTab(
                'Root.Main',
                // Please follow the "ManyMany[<extradata-name>]" convention
                new TextField('ManyMany[Motion]', 'Agree with Motion')
            );
            $config = GridFieldConfig_RelationEditor::create();
            $config->getComponentByType('GridFieldDetailForm')->setFields($panelistFields);
            $gridField = new GridField('Panelists', 'Panelists', $this->Panelists(), $config);
            $fields->findOrMakeTab('Root.Panelists')->replaceField('Panelist', $gridField);
        }        
    }

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    您可以在 $many_many 关系上使用 $many_many_extraFields,就像这样(我猜是在 Project 类上):

    static $many_many = array(
        'Mentors' => 'Mentor'
    );
    
    static $many_many_extraFields = array(
        'Mentors' => array(
            'Active' => 'Boolean'
        )
    );
    

    然后,对于每个 Project,特定的 Mentor 可以处于活动状态或不处于活动状态(您始终可以添加除'Active'...之外的其他字段。

    如果您使用的是 SS 3.1,您可以通过带有 GridFieldDetailForm 组件的 GridField 轻松编辑这些额外的字段:

    function getCMSFields(){
    
        --[snip]--
    
        $detailFormFields = new FieldList();
        $detailFormFields->push( new CheckBoxField(
            'ManyMany[Active]',
            'Is Mentor active?'
        ));
        $detailFormFields->push( new TextField(
            'SomeOtherField',
            'Some other title'
        ));
        $config = new GridFieldConfig_RelationEditor();
        $config->getComponentByType('GridFieldDetailForm')->setFields($detailFormFields);
    
        $f = new GridField('Mentors', 'Mentors', $this->Mentors(), $config);
        //push() or addFieldToTab() $f to CMSFields
    
        --[snip]--
    
    }
    

    文档在这里:http://doc.silverstripe.com/framework/en/3.1/reference/grid-field#customizing-detail-forms

    在您的代码中检索数据时,您可以使用ManyManyList 上的getExtraData($componentName, $itemID) 方法来检索那些额外的字段值: http://api.silverstripe.org/3.1/source-class-ManyManyList.html#178-210

    【讨论】:

    • 好的,谢谢。我在 3.0 上,但看起来这可能在那里也可用,对吗?有这方面的任何文件吗?不能让它工作
    • $config 变量 - 那是什么。它导致“在非对象上调用成员函数 getComponentByType()”错误
    • 通过 GridField 编辑 ManyMany 字段是在 SS 3.1 beta 1 中引入的,因此如果您使用的是 3.0,则不能使用它。您可以查看提交并将其移植到 3.0 (github.com/silverstripe/silverstripe-framework/commit/…) 或升级到 3.1,这可能是一个好主意并且不需要太多。 $config var 是创建 GridField 的一部分,它是一个非常短的 sn-p,所以之前没有所有部分,我用 GridField 的整个代码编辑了上面的答案并添加了指向文档。
    • 谢谢。我已经升级到 3.1 并在源代码中找到了一些示例并尝试了它。我得到“我无法处理 CMSForm 对象的子 URL”。错误。我现在就检查一下你的 sn-p,看看它是否能有所启发
    • 您能否在这里发布 DebatePage.php 和 Panelist.php 的完整代码,或者作为 Gist 更好。可能能够更好地测试和帮助?
    【解决方案2】:

    (参见:SS3.1 - http://api.silverstripe.org/3.1/class-CheckboxSetField.html

    试试这个:

    private static $many_many = array(
        'Mentors' => 'Mentor'
    ); 
    $mentors = Mentor::get();   
    $mentorFields = new CheckboxSetField(    
        'Mentors',   
        'Mentor',   
        $mentors->map(),   
        $value="1"   
    );  
    $fields->addFieldToTab('Root.Mentors', $mentorFields);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多