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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo

using DataObject::get() to fill dropdown in getCMSFields_forPopup()


Go to End
Reply


9 Posts   8805 Views

Avatar
RobertM

Community Member, 26 Posts

22 August 2008 at 1:04am

I have created these two classes, but I would like to populate the dropdown in Project from another DataObject table that can be edited inside a tab in ProjectHolder.

class ProjectHolder extends Page {
   static $db = array(
   );
   static $has_one = array(
      
   );
    static $has_many = array(
      'Projects' => 'Project'
   );

   function getCMSFields() {
      $fields = parent::getCMSFields();

	$tablefield = new HasManyComplexTableField(
         $this,
         'Projects',
         'Project',
         array(
	    'title' => 'Title',
	    'manager' => 'Manager'
         ),
         'getCMSFields_forPopup'
      );
      $tablefield->setAddTitle( 'A Project' );

	$fields->addFieldToTab( 'Root.Content.Projects', $tablefield );

      return $fields;
   }
}

class Project extends DataObject {

    static $db = array(
      'title' => 'Text',
	'manager' => "Enum('Manager1, Manager2')" 
   );
 
   static $has_one = array(
      'MyProjectHolder' => 'ProjectHolder'
   );

   function getCMSFields_forPopup() {
	$fields = new FieldSet();
      	$fields->push( new TextField( 'title' ) );
	$map = array('1' =>'Manager1','2' => 'Manager2');
	$fields->push( new DropdownField(
		'manager', 
		'manager of project',   	
		$map) 
	);
	
      return $fields;
   }
 
}

Avatar
Willr

Forum Moderator, 5516 Posts

22 August 2008 at 3:09pm

So you want to populate the 'Managers' list on Project.php from a seperate 'Managers' DataObject? Well What you would need to do is create a Managers DataObject first off, then a project has one Manager. Then I think you can call toArray() on a dataobjectset. So you would have


class Project extends DataObject {
static $db = array( 
'title' => 'Text', 
);

static $has_one = array( 
'MyProjectHolder' => 'ProjectHolder',
'Manager' => 'Manager'
);

function getCMSFields_forPopup() { 
   $fields = new FieldSet(); 
   $fields->push( new TextField( 'title' ) ); 
   $managers = DataObject::get("Manager")->toArray();
   $fields->push( new DropdownField( 
      'manager', 
      'manager of project',     
      $managers) 
   ); 
    
return $fields; 
}

} 

That would be your project code. You would also then need a Manager.php dataobject.

Avatar
RobertM

Community Member, 26 Posts

22 August 2008 at 9:38pm

i'm still having a bit of trouble with this, as it is not populating the dropdown box with the contents of DataObject::get("Manager"), but merely giving one option, "Manager". The second problem is that even if this only option "Manager" is chosen it doesn't save - before I had

 'manager' => "Enum('Manager1, Manager2')" 

in $db.

my files now look thus:

<?php

class Project extends DataObject {

    static $db = array(
      'title' => 'Text',
	'manager' => 'Text'
   );
 
   static $has_one = array(
      'MyProjectHolder' => 'ProjectHolder',
	'Manager' => 'Manager'
   );

   function getCMSFields_forPopup() {
	$fields = new FieldSet();
      	$fields->push( new TextField( 'title' ) );
	$managers = DataObject::get("Manager")->toArray();
	$fields->push( new DropdownField(
	'manager',
	'manager of project',
	$managers)
	);
	
      return $fields;
   }
 
}

?>

<?php

class Manager extends DataObject {

    static $db = array(
      'Name' => 'Text'
   );
 
   static $has_one = array(
      'MyProjectHolder' => 'ProjectHolder'
   );

   static $has_many = array(
	'MyProject' => 'Project'
   );

   function getCMSFields_forPopup() {
	$fields = new FieldSet();
      	$fields->push( new TextField( 'Name' ) );
	
      return $fields;
   }
 
}

?>

<?php
/**
 * Defines the ProjectHolder page type
 */
class ProjectHolder extends Page {
   static $db = array(
   );
   static $has_one = array(
      
   );
    static $has_many = array(
      'Projects' => 'Project',
	'Managers' => 'Manager'
   );

   function getCMSFields() {
      $fields = parent::getCMSFields();

	$tablefield = new HasManyComplexTableField(
         $this,
         'Projects',
         'Project',
         array(
	    'title' => 'Title',
	    'manager' => 'Manager'
         ),
         'getCMSFields_forPopup'
      );
      $tablefield->setAddTitle( 'A Project' );

	$managertablefield = new HasManyComplexTableField(
         $this,
         'Managers',
         'Manager',
         array(
	    'name' => 'Name',
	   
         ),
         'getCMSFields_forPopup'
      );
      $managertablefield->setAddTitle( 'A Manager' );

	$fields->addFieldToTab( 'Root.Content.Projects', $tablefield );
	$fields->addFieldToTab( 'Root.Content.Managers', $managertablefield );
      return $fields;
   }
}

class ProjectHolder_Controller extends Page_Controller {
}

perhaps i'm doing something wrong?

Avatar
Willr

Forum Moderator, 5516 Posts

22 August 2008 at 9:47pm

Well one of the issues that its not saving is because we defined a has_one relation on Project -> manager we dont need the manager database field (in the $db array) Instead the has_one automatically creates a FieldNameID database column we want to save into for the has_one so you would need to change the dropdown constructor

  $fields->push( new DropdownField( 
   'ManagerID', 
   'manager of project', 
   $managers) 
   ); 

as for only 1 manager showing up you might just like to do a Debug::show($managers); after the DataObject() call and see what it returns. See if anythings amiss

Avatar
RobertM

Community Member, 26 Posts

22 August 2008 at 10:23pm

It still wont save, but I do get 2 options now, but they appear as "Manager" still in the dropdown. Here is the output of Debug::show($managers);

Debug (Project->getCMSFields_forPopup() in line 18 of Project.php)

    * 0 =
      Database record: Manager
          o ID :

            4

          o ClassName :

            Manager

          o Created :

            2008-08-22 11:16:21

          o LastEdited :

            2008-08-22 11:16:53

          o Name :

            Manager1

          o MyProjectHolderID :

            11

          o RecordClassName :

            Manager

    * 1 =
      Database record: Manager
          o ID :

            5

          o ClassName :

            Manager

          o Created :

            2008-08-22 11:16:38

          o LastEdited :

            2008-08-22 11:16:53

          o Name :

            Manager2

          o MyProjectHolderID :

            11

          o RecordClassName :

            Manager

Avatar
RobertM

Community Member, 26 Posts

22 August 2008 at 11:15pm

I have sorted the problem with getting the dropdown populated thanks to your tip about debug, but I still have the problem that it doesn't save the dropdown selection - title is save but manager stays blank. Here is my code:

<?php

class Project extends DataObject {

    static $db = array(
      'title' => 'Text'
   );
 
   static $has_one = array(
      'MyProjectHolder' => 'ProjectHolder',
	'Manager' => 'Manager'
   );

   function getCMSFields_forPopup() {
	$fields = new FieldSet();
      	$fields->push( new TextField( 'title' ) );
	$managers = DataObject::get("Manager");
	$map = $managers->toDropDownMap('ID', 'Name');
	$fields->push( new DropdownField(
	'ManagerID',
	'manager of project',
	$map)
	);
	
      return $fields;
   }
 
}

?>

Avatar
Willr

Forum Moderator, 5516 Posts

23 August 2008 at 12:17pm

hmm strange. If you look at the source code of the drop down is there a 'value="X"' in each option field? eg is there something to save

Avatar
RobertM

Community Member, 26 Posts

24 August 2008 at 2:22am

yes it appears so, here is the source for the form:

<form id="ComplexTableField_Popup_DetailForm" action="admin/?executeForm=EditForm.ReferencedField.DetailForm&fieldName=Projects" method="post" enctype="application/x-www-form-urlencoded">

	
	<p id="ComplexTableField_Popup_DetailForm_error" class="message " style="display: none"></p>
	
	<fieldset>
		
			<div id="title" class="field text "><label class="left" for="ComplexTableField_Popup_DetailForm_title">title</label><span class="middleColumn"><input class="text" type="text" id="ComplexTableField_Popup_DetailForm_title" name="title" value="" /></span></div>

		
			<div id="ManagerID" class="field dropdown "><label class="left" for="ComplexTableField_Popup_DetailForm_ManagerID">manager of project</label><span class="middleColumn"><select   name="ManagerID" id="ComplexTableField_Popup_DetailForm_ManagerID"><option value="4">ManagerR</option><option value="5">Manager2</option></select></span></div>
		
			<input class="hidden" type="hidden" id="ComplexTableField_Popup_DetailForm_ctf-parentClass" name="ctf[parentClass]" value="ProjectHolder" />
		
			<input class="hidden" type="hidden" id="ComplexTableField_Popup_DetailForm_ctf-ClassName" name="ctf[ClassName]" value="Project" />
		
			<input class="hidden" type="hidden" id="SecurityID" name="SecurityID" value="1370550787" />
		
		<div class="clear"><!-- --></div>
	</fieldset>


	<div class="Actions">
			<input class="action  save" id="ComplexTableField_Popup_DetailForm_action_saveComplexTableField" type="submit" name="action_saveComplexTableField" value="Save"  />

	</div>


</form>

Go to Top