Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

manage 'belongs_to'' using modeladmin


Go to End


2 Posts   1410 Views

Avatar
thomas.paulson

Community Member, 107 Posts

22 July 2015 at 5:31pm

I have create a Member dataextension as below, is there any way to display 'MemberContact' details on other tab and also edit them,

<?php
class MemberExtension extends DataExtension {

  private static $db = array(
	'Gender' => 'Varchar(2)',
    'MartialStatus' => 'Varchar(2)',
    'DateOfBirth' => 'Date'
  );


  private static $has_one = array(
    'Caste' => 'Caste'
  );

  
  private static $belongs_to = array(
       'MemberContact' => 'MemberContact.Member'
  );
   
  
  public function updateCMSFields(FieldList $fields) {    
  }

}
class MemberContact extends DataObject {

  private static $db = array(
	'ContactNumber' => 'Varchar(100)',
	'Address' => 'Varchar(255)',
	'City' => 'Varchar(100)',
	'District' => 'Varchar(100)',
	'CreatedBy' => "Enum(array('self', 'parent', 'sibling', 'relative', 'friend'))"
  );

  private static $has_one = array(
    'Member' => 'Member'
  );

}

class MemberAdmin extends ModelAdmin
{
	
    private static $managed_models = array(
        'Member','Caste','Course','JobCategory'        
    );	    

    private static $url_segment = 'members';

    private static $menu_title = 'Member Admin';
    
    // ...
    public function getEditForm($id = null, $fields = null) {
        $form = parent::getEditForm($id, $fields);

        // $gridFieldName is generated from the ModelClass, eg if the Class 'Product'
        // is managed by this ModelAdmin, the GridField for it will also be named 'Product'

        $gridFieldName = $this->sanitiseClassName($this->modelClass);
        $gridField = $form->Fields()->fieldByName($gridFieldName);

        // modify the list view.
        //$gridField->getConfig()->addComponent(new GridFieldFilterHeader());
        
        //
        if($this->modelClass=='Course' || $this->modelClass=='JobCategory' ){
			$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));			
		}

        return $form;
    }    
}

Avatar
Pyromanik

Community Member, 419 Posts

22 July 2015 at 9:56pm

Not sure.
But two small things.

I'm not 100% sure but shouldn't the belongs_to just specify 'MemberContact' rather than 'MemberContact.Member'?
Although that doesn't really matter as it is basically just more specific.

And GridField (thus ModelAdmin) is for managing lists of data, which has_one does not supply. If you're talking about the single record edit form (detailform in gridfield terminology, iirc), then you might be wanting something like simon_w's has_one edit field. http://addons.silverstripe.org/add-ons/simonwelsh/hasoneedit