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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Where to use DataObjectManager?


Go to End
Reply


10 Posts   2366 Views

Avatar
patte

Community Member, 63 Posts

24 July 2009 at 4:54pm

Hey guys,

I am using DataObjectManager for my first project using dataobjects. Can someone please give a hint where/how to use it?

I have two dataobjects 'CertificationBody' and 'Certificate' - a one to many relationship.

My question is: where do i use DataObjectManager? Until now I dont have a new page type to put it in and I thought that those page types must be in relation with the dataobjects... is that right? Or can I build a new Page just for administration?

Thanks much in advance!
patte

Avatar
UncleCheese

Forum Moderator, 4096 Posts

24 July 2009 at 6:34pm

It works almost exactly like a ComplexTableField, relating DataObjects to Page objects. If you have a DataObject -> DataObject relation, you might want to try it in ModelAdmin.

Avatar
patte

Community Member, 63 Posts

3 August 2009 at 4:36am

Hi UncleCheese,

thanks for your answer. Perhaps you can help me with some problems I have with my dataobjects? As I wrote these are my first steps with SilverStripe an dataobjects and I am stuck with some things... the tutorials didnt`t help unfortunately...

IsccPage is my administration page for Certificate, CertificationBody and Region.

A CertificationBody has Certificates - so a Certificate belongs to one CertificationBody. Both have a region, which should be selectable in any way from within DOM (CheckboxSetField or DropdownField?).


class IsccPage extends Page
{
	static $has_many = array (
		'Certificates' => 'Certificate',
		'CertificationBodies' => 'CertificationBody',
		'Regions' => 'Region'
	);



   function getCMSFields() {
      $fields = parent::getCMSFields();      
      
      $fields->addFieldToTab("Root.Content.Certificates", new DataObjectManager(
 	$this,
         'Certificates',
         'Certificate',
         array(
	    'CertificateNumber' => 'Certificate Number',
	    'DateOfIssue' => 'Date of Issue',
	    'DateOfExpiration' => 'Date of Expiration',
	    'CompanyCertified' => 'Company Name'
         ),
         'getCMSFields_forPopup'
	));
     
      
      $fields->addFieldToTab("Root.Content.CertificationBodies", new DataObjectManager(
         $this,
         'CertificationBodies',
         'CertificationBody',
         array(
	    'CompanyName' => 'Company Name',
	    'ContactPerson' => 'Contact Person',
	    'Telephone' => 'Telephone',
	    'Email' => 'Email'
         ),
         'getCMSFields_forPopup'
      ));
  
      
	$fields->addFieldToTab("Root.Content.Regions", new DataObjectManager(
		$this,
		'Regions',
		'Region',
		array('Title' => 'Title'),
		'getCMSFields_forPopup'
	));

	return $fields;
   }

}

class Certificate extends DataObject {
	
	static $db = array (
		'CertificateNumber' => 'Text',
		'DateOfIssue' => 'Date',
		'DateOfExpiration' => 'Date',
		'CompanyCertified' => 'Text',
		'LocationOfCompany' => 'Text',
		'Country' => 'Text'
	);
	
	static $has_one = array (
		'IsccPage' => 'IsccPage',
		'Region' => 'Region'
	);
	
	function getCMSFields_forPopup() {
      	$fields = new FieldSet();
      	$fields->push( new TextField('CertificateNumber') );
      	$fields->push( new CalendarDateField('DateOfIssue') );
      	$fields->push( new CalendarDateField('DateOfExpiration') );
      	$fields->push( new TextField('CompanyCertified') );
      	$fields->push( new TextField('LocationOfCompany') );
      	$fields->push( new TextField('Country') );      	
      	
      	$regionsList = DataObject::get('Region');
	$fields->push(new CheckboxSetField('Regions', '', $regionsList));     	
      	
      	return $fields;
   	}
}

class CertificationBody extends DataObject {
	
	static $db = array (
		'CompanyName' => 'Text',
		'FullAddress' => 'Text',
		'Country' => 'Text',
		'ContactPerson' => 'Text',
		'Telephone' => 'Text',
		'Telefax' => 'Text',
		'Email' => 'Text',
		'Website' => 'Text',
		'CertificationProducts' => 'Text',
		'Region' => 'Text',
		'CertifiedSince' => 'Date'
	);

	static $has_one = array(
		'IsccPage' => 'IsccPage'
	);
		
	static $has_many = array (
		'Certificates' => 'Certificate'
	);
	
	function getCMSFields_forPopup() {
      	$fields = new FieldSet();
      	
      	$fields->push( new TextField('CompanyName') );
      	$fields->push( new CalendarDateField('CertifiedSince') );
      	$fields->push( new TextareaField('FullAddress') );
      	
	$fields->push( new TextField('Country') );
	$fields->push( new TextField('ContactPerson') );
	$fields->push( new TextField('Telephone') );
	$fields->push( new TextField('Telefax') );
	$fields->push( new TextField('Email') );
	$fields->push( new TextField('Website') );
	$fields->push( new TextField('CertificationProducts') );
	$fields->push( new TextField('Region') );
		
	$certList = DataObject::get('Certificate')->toArray();
	$fields->push( new DropdownField(
	'CertificateID',
	'Certificates',
	$certList)
	); 
		
		
	$regionList = DataObject::get('Region');
	$fields->push(new CheckboxSetField('Regions', '', $regionList));
		     	
      	return $fields;
   	}
}



class Region extends DataObject {
	

   static $db = array(
      'Title' => 'Varchar'
   );
 
   static $belongs_many_many = array(
      'IsccPages' => 'IsccPage',
      'Certificates' => 'Certificate',
      'CertificationBodies' => 'CertificationBody'
   );

	
}

puhh... i got the DOM working but..

Adding a CertificationBody I can select a Region but the form is not saving the value. Editing the Object shows no selection of a region.

Within CertificationBody I tried to use DropdownField for the Certificate but i shows only 'certificate' for any entry.

How can I select a CertificationBody from within a Certificate?

Whats wrong with my dataobjects? I am in a big muddle I think and it would very very great if someone can enlighten me ;-)

Thanks so much in advance!

Patte

Avatar
UncleCheese

Forum Moderator, 4096 Posts

3 August 2009 at 11:48am

How come region has belongs_many_many, but that relation is not reciprocated in any of those three classes listed? If an object has a belongs_many_many then the class in that relation has to have a many_many to that object. Further, you need to use ManyManyDataObjectManager to manage that relation in lieu of a normal DOM.

Avatar
patte

Community Member, 63 Posts

4 August 2009 at 2:57am

Hi UncleCheese,

...thanks much. With belongs_many_many and many_many I got the regions checkboxes working ;-) Perhaps you have an idea why my DropdownField for Certificate is not saving its value?

I started from the beginning using your TestimonialPage example just to be sure not to make a mistake


class TestimonialPage extends Page
{
	static $has_many = array (
		'Testimonials' => 'Testimonial'
	);
	
	public function getCMSFields()
	{
		$fields = parent::getCMSFields();
		$manager = new DataObjectManager(
			$this, // Controller
			'Testimonials', // Source name
			'Testimonial', // Source class
			array('Date' => 'Date', 'Author' => 'Author', 'Quote' => 'Quote'), // Headings
			'getCMSFields_forPopup' // Detail fields function or FieldSet
			// Filter clause
			// Sort clause
			// Join clause
		);
		
		
		$fields->addFieldToTab("Root.Content.Testimonials", $manager);
		
		$fields->addFieldToTab("Root.Content.Main",new DropdownField('Testimonials','Please choose one', Dataobject::get("Testimonial")->toDropdownMap("ID", "Title")));				
		
		return $fields;
	}

}

I added addFieldToTab to get the DropDownMap for the Testimonal - I can select one Testimonial but the value is not being saved.

Any idea?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

4 August 2009 at 3:35am

Why are you using a DropdownField to manage a has_many relationship? That makes no sense. A dropdown field can only select one value.

Avatar
patte

Community Member, 63 Posts

4 August 2009 at 4:04am

Thank you for you patience UncleCheese,
I just wanted to test the DropdownField...being totally confused with that dataobject relations.

I have to do the following things - perhaps my whole approach is wrong?

CertificationBody has many Certificates
CertificationBody has one Region

Certificate has one CertificationBody
Certificate has one Region

I would like to have a 'holder' for my dataobjects, call it IsccPage. In this page i want to use DOM.

Within CertificationBody DOM I want to have a CheckBox to select several Certificates and a DropdownField to select one Region
Within the Certificate DOM i want to have a DropdownField to select one Region and one CertificationBody

..

This is possible isn`t it? Because my client wants to see a first version today I am getting nervous..... can you give me some more hints whats going wrong with my attempts?

Thanks much

Avatar
UncleCheese

Forum Moderator, 4096 Posts

4 August 2009 at 4:30am

Yeah, of course..

For CertificationBody, just add a CheckboxSetField('Certificates') to your popup fields. Also put a dropdown field there for RegionID.

On Certificate, have a dropdown for CertificationBodyID (should be already set if you've assigned it via the checkboxes on CertificationBody), and a dropdown for RegionID.

Go to Top