2013-07-30 3 views
5

는 :

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

나는 관계에 대한 속성을 기록하고 싶습니다 . 그래서 예를 들어 "active"- 프로젝트의 멘토를위한 예/아니오 필드. 그러나 멘토는 자신이 관련된 여러 프로젝트에 대해 다른 가치를 가질 수 있습니다.

Silverstripe에 내장 된 도구로이를 수행하는 가장 좋은 방법은 무엇입니까?

업데이트 아래의 답변은 IRC &의 도움을 받았습니다. ive는 조금 더 가깝게되었고, 비트는 작동하지 않았습니다. 필자는 이것을 찾았습니다 : https://github.com/chillu/silverstripe-framework/blob/c8136f5d4c8a37a4da274cd1c93907c0a2af86a7/docs/en/reference/grid-field.md 매우 관련있는 것 같습니다.

그래서 DebatePages에는 각 토론에서 다르게 투표 할 수있는 많은 패널 토론자가 있습니다.

DebatePage.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); 
     }   
    } 

답변

6

당신이합니다 (프로젝트 같아요 클래스에 여기)이처럼 $many_many 관계에 $many_many_extraFields을 사용할 수 있습니다 각 프로젝트에 대한 다음

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

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

특정 멘토은 활성화되거나 비활성화 될 수 있습니다 (항상 'Active' 이외의 다른 필드를 추가 할 수 있음). ...).

당신이 사용하는 경우 당신이 GridFieldDetailForm 구성 요소와 GridField를 통해 쉽게 편집 그 여분의 필드를 가질 수 3.1 SS : 이것에

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에서 http://api.silverstripe.org/3.1/source-class-ManyManyList.html#178-210

를 검색하려면 ManyManyList에서 getExtraData($componentName, $itemID) 메서드를 사용할 수 있습니다. 16,
+0

확인 감사합니다. 나는 3.0에 있지만 어쩌면 이것이 너무 유효 할 것 같습니다. 이것에 대한 문서가 전혀 없습니까? 꽤 잘 작동하지 않습니다 – Will

+0

$ config var - 그게 뭐죠? 그 원인은 "부재 개체 getComponentByType()에 대한 호출"오류 – Will

+0

GridField를 통해 ManyMany 필드를 편집하는 것은 SS 3.1 베타 1에서 도입되었으므로 3.0에서 사용하면 사용할 수 없습니다. 커밋을보고 3.0 (https://github.com/silverstripe/silverstripe-framework/commit/c8136f5d4c8a37a4da274cd1c93907c0a2af86a)으로 포팅하거나 3.1로 업그레이드하면 좋은 생각 일 수 있습니다. '$ config' var는 GridField를 생성하는 부분입니다. 이전에는 모든 부분이 없었기 때문에 매우 간단한 코드 조각이었습니다. 위의 대답을 GridField의 전체 코드로 편집하고에 대한 링크를 추가했습니다. 의사.덕분에 – colymba