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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

┬žsummary_fields & $has_one problem


Go to End


2 Posts   1922 Views

Avatar
darwin

Community Member, 6 Posts

10 October 2012 at 5:53am

Edited: 10/10/2012 5:54am

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';
}

Avatar
Optic Blaze

Community Member, 190 Posts

14 October 2012 at 12:27pm

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;
}

}