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

ModelAdmin DataObjects


Go to End


2 Posts   1522 Views

Avatar
CodeFade

Community Member, 12 Posts

30 August 2015 at 11:19pm

Edited: 30/08/2015 11:21pm

Hi,

I've created a DataObject called ClinicalCenter, then created a ModelAdmin for it, added couple of Clinical Centers, and everything seams to work perfectly. Then created a PageHolder called ClinicalCentersPage to fetch and list all these centers, but nothing appears on the front end.

Appreciate your help guys.

Here is my codes

ClinicalCenter.php

class ClinicalCenter extends DataObject {

	private static $db = array(
		'Title' => 'Varchar',
		'Description' => 'HTMLText',
		'Active' => 'Boolean'
	);


	private static $has_one = array(
		'FeaturedImage' => 'Image',
		'ClinicalCentersPage' => 'ClinicalCentersPage'
	);

	
	private static $summary_fields = array(
		// summary fields goes here ...
	);



	public function getCMSFields() {
		
		$fields = FieldList::create(TabSet::create('Root'));
		
		$fields->addFieldsToTab('Root.Main', array(
			
			TextField::create('Title', 'Clinical Center Name'),
			HtmlEditorField::create('Description', 'Describe this Clinical Center'),
			CheckboxField::create('Active', 'Is the Center Active?'),
			$upload = UploadField::create('FeaturedImage', 'Featured Image')
		
		));
		
		$upload->getValidator()->setAllowedExtensions(array(
			'png', 'jpg', 'jpeg', 'gif'
		));
		
		$upload->setFolderName('clinical-images');

		return $fields;
	}
}

ClinicalCentersAdmin.php

class ClinicalCentersAdmin extends ModelAdmin {
	
	private static $menu_title = 'Clinical Centers';
	
	private static $url_segment = 'clinical-centers';
	
	private static $managed_models = array(
		'ClinicalCenter'
	);

	private static $menu_icon = 'mysite/icons/medical-icon.png';
}

ClinicalCentersPage.php

class ClinicalCentersPage extends Page {

    private static $has_many = array(
        'ClinicalCenters' => 'ClinicalCenter'
    );

}

class ClinicalCentersPage_Controller extends Page_Controller {
}

ClinicalCentersPage.ss

<div class="row">

<% loop $ClinicalCenters %>

	<div class="col-lg-3">
		<div class="box">
			<div class="box-gray aligncenter">
				<h4>$Title</h4>
				<div class="icon">
					$FeaturedImage.CroppedImage(242,156)
				</div>
				<p>
				 $Description.FirstSentence
				</p>
			</div>
			<div class="box-bottom">
				<a href="$Link">View</a>
			</div>
		</div>
	</div>

<% end_loop %>

</div>

Please help guys, where I'm going wrong?

Avatar
thomas.paulson

Community Member, 107 Posts

1 September 2015 at 7:08pm

Though you created the data-relation on ClinicalCenter with ClinicalCentersPage, they are not shown in cms section via getCMSFields, so i would recommend you add the below mentioned code to link the ClinicalCenter with ClinicalCentersPage,

$page = DropdownField::create('ClinicalCentersPageID', 'ClinicalCentersPage', ClinicalCentersPage::get()->map('ID', 'Title'))

class ClinicalCenter extends DataObject {

	private static $db = array(
		'Title' => 'Varchar',
		'Description' => 'HTMLText',
		'Active' => 'Boolean'
	);


	private static $has_one = array(
		'FeaturedImage' => 'Image',
		'ClinicalCentersPage' => 'ClinicalCentersPage'
	);

	
	private static $summary_fields = array(
		// summary fields goes here ...
	);



	public function getCMSFields() {
		
		$fields = FieldList::create(TabSet::create('Root'));
		
		$fields->addFieldsToTab('Root.Main', array(
			
			TextField::create('Title', 'Clinical Center Name'),
			HtmlEditorField::create('Description', 'Describe this Clinical Center'),
			CheckboxField::create('Active', 'Is the Center Active?'),
			$upload = UploadField::create('FeaturedImage', 'Featured Image'),
		        $page = DropdownField::create('ClinicalCentersPageID', 'ClinicalCentersPage', ClinicalCentersPage::get()->map('ID', 'Title'))              
		));
		
		$upload->getValidator()->setAllowedExtensions(array(
			'png', 'jpg', 'jpeg', 'gif'
		));
		
		$upload->setFolderName('clinical-images');

		return $fields;
	}
}