1
votes

In SilverStripe 3.1.13 I'm trying to establish a simple one-to-many relationships between DataObjects administrated by ModelAdmin. There is a Facility class that can have one off FacilityCategory.

I can enter the Facility Category tab, but as soon as I enter Facility tab, the "main" screen goes blank, and the admin gets contaminated from then on - no tab would show contents anymore.

I erased all the tables beginning with Facility and FacilityCategories and did dev/build repeatedly with flush.

Would anybody shed some light on me as per why it does not work? What is wrong about my classes/relations?

Facility.php

class Facility extends DataObject {
    private static $db = array(
        'Title' => 'Varchar',
    );

    private static $has_one = array(
        'Category' => 'FacilityCategory'
    );

    public static $summary_fields = array(
        'Title', 'Category'
    );



    public function getCMSFields(){
        $fields = FieldList::create(
            TextField::create('Title'),
            DropdownField::create('FacilityCategoryID', 'Category')
                ->setSource(FacilityCategory::get()->map('ID', 'Title'))
                ->setEmptyString('-- select a category --')
        );
        return $fields;
    }
}

class FacilityAdmin extends ModelAdmin {
    private static $menu_title = 'Facilities';
    private static $url_segment = 'facilities';
    private static $managed_models = array(
        'Facility'
    );
}

FacilityCategory.php

class FacilityCategory extends DataObject {
    private static $db = array(
      'Title' => 'Varchar'
    );

    private static $has_many = array(
      'Facilities' => 'Facility'
    );

    public function getCMSFields(){
        $fields = FieldList::create(
            TextField::create('Title')
        );
        return $fields;
    }
}

class FacilityCategoryAdmin extends ModelAdmin {
    private static $menu_title = 'Facility Categories';
    private static $url_segment = 'facility-categories';
    private static $managed_models = array(
        'FacilityCategory'
    );
}
1
It could be that in your $summary_fields (which is expected to be private), you have listed the related category object rather than a particular field on the category object, so have private static $summary_fields = array('Title', 'Category.Title');. Also, any particular reason why you are using 2 separate modelAdmins? I'd tend to use the same ModelAdmin to manage both models (as they are related).jpmcc
@jpmcc Bull's eye! That was it. A very good lesson. Please copy your comment as an answer and you'll get it accepted.user776686
As per two separate ModelAdmins - this was my first intuitive choice to keep the admin stuff in the same file along with the corresponding DataObject class, but if I tell me that such an approach, for instance, affects performance, I will surely go your way.user776686
Sorry, wasn't trying to be picky or anything. Not sure about performance.jpmcc

1 Answers

2
votes

It could be that in your $summary_fields (which is expected to be private), you have listed the related category object rather than a particular field on the category object, so have:

private static $summary_fields = array(
  'Title', 
  'Category.Title'
);