Jump to:

3432 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » §summary_fields & $has_one problem

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

Page: 1
Go to End
Author Topic: 732 Views
  • darwin
    Avatar
    Community Member
    5 Posts

    §summary_fields & $has_one problem Link to this post

    Hey folks,

    I'm building a phpMyAdmin-like backend for my smartphone applications.

    I have 4 Models:

    - Band
    - News
    - Konzert
    - Release

    Each News / Konzert / Release $has_one Band, so if I add a new e.g. News there should be a DropdownField with all Bands. This works but it just shows me #1, #2, ... instead of the Band's title.

    Second problem: The fields are shown as columns in a GridView but the aliases do not work ...

    Hoping so. can help me.

    Some Code:

    class Konzert extends DataObject {

       // Contact object's fields
       public static $db = array(
          'doorsopen' => 'Text'
       );
       
       /*public static $has_one = array(
          'Band' => 'Band'
       );*/

       // Summary fields
       public static $summary_fields = array(
          'doorsopen' => 'Doors-Open' //shows "doorsopen" not "Doors-Open"
       );

       public function getCMSFields_forPopup() {

          // Name, Description and Website fields
          return new FieldList(
             new TextField('doorsopen', 'Doors-Open')
          );
       }

    }

    class AppModelAdmin extends ModelAdmin {
    public static $managed_models = array('News', 'Band', 'Konzert', 'Release');
    static $url_segment = 'app';
    static $menu_title = 'App-Verwaltung';
    }

  • Optic Blaze
    Avatar
    Community Member
    161 Posts

    Re: §summary_fields & $has_one problem Link to this post

    Ok i had a look at the code. To fix your problems you need to look at the following. I tested it om my side and it works.

    ------------------------------------------------------
    Konzert.php
    -------------------------------------------------------

    class Konzert extends DataObject {
       static $db=array(
          'KonzertName'=>'Varchar(150)',
          'DoorsOpen'=>'Varchar(150)'
          );
       static $has_one = array(
       // Left side is the name of relationship --- right hand side is the class is related to. This has_one relationship creates a column called
       // BandsID in our Konzert table to show Silverstripe that there is a relationship between the two classes
       'Bands'=>'Band'
       );

       // Sets the sumary fields that will be displayed in the grid field
       static $summary_fields = array(
    'KonzertName',
        'DoorsOpen'
        );
       
       function getCMSFields() {   
       // to populate drowp down field for 'Band' it needs a source. We then 'map' the 'ID' col as the the one used to store values in database
       // and 'Name' is the name of the band ---- see code for 'Band.php'
       $BandDropdown = Dataobject::get("Band")->map("ID", "Name");

       // create new field list and remember we need to add our has_one field 'Band' in as well
       $fields = new FieldList (   
       
       new TextField('KonzertName', 'Name of the Konzert'),
       new TextField('DoorsOpen', 'Doors Open'),
       // The drop down field has 4 parameters:
          // 1) Name of the field that we are saving to. In our case it is has one relationship called 'Bands' that created a <br />
    // field called BandsID in the table called Konzeret.
          // 2) The description 'Bands Playing'
          // 3) The values we want in our drop down menu we created above
          // 4) We leave params 4 and 5 open and set the deefault message for the drop down to 'Please select'
       new DropdownField('BandsID', 'Bands Playing', $BandDropdown,'','','--Please Select--')
       );
       return $fields;
       }
       
       // Now we set validation for our Data Object Fields
       public function getCMSValidator() {
    return new RequiredFields(
       'KonzertName',
       // Remember our drop down field references the has_one relationship with the field BandsID
       'BandsID',
       'DoorsOpen'
       );
    }
    }

    ---------------------------------------------------
    Band.php
    ----------------------------------------------------

    class Band extends DataObject {
       static $db=array(
          'Name'=>'Varchar(150)'

          );

       function getCMSFields() {
       $fields = new FieldList (                  
       new TextField('Name', 'Band Name')
       );
       return $fields;
       }
       
    }

    732 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.