1

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'
    );
}
user776686
  • 7,933
  • 14
  • 71
  • 124
  • 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 Aug 15 '15 at 17:27
  • @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 Aug 15 '15 at 18:41
  • 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 Aug 15 '15 at 18:44
  • Sorry, wasn't trying to be picky or anything. Not sure about performance. – jpmcc Aug 15 '15 at 22:34

1 Answers1

2

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'
);
Turnerj
  • 4,258
  • 5
  • 35
  • 52
jpmcc
  • 368
  • 5
  • 11
  • Just one extra thing to add to this, you may likely want to change the label for the "Category.Title" summary field to something else. [Use `$field_labels`](https://docs.silverstripe.org/en/3.1/developer_guides/customising_the_admin_interface/modeladmin/#displaying-results) as there are some messy edge-cases when specifying labels in the `$summary_fields`. – Turnerj Aug 16 '15 at 22:14
  • 1
    Good point. I'd agree with that. Also very useful if you ever need to localise. – jpmcc Aug 17 '15 at 08:11