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.

Data Model Questions /

modelAdmin -> receive data for Dropdown


Reply


4 Posts   2374 Views

Avatar
dacar

Community Member, 168 Posts

17 August 2009 at 10:58pm

Edited: 17/08/2009 10:59pm

Hi,

with the following code i want to edit my staff members. Each of this members is related to one specific company.
On the admin page there is a dropdown that showes the ID from an already existing company. How can i change the dropdown to show the Name (a field from DB) of my company dataObjects?

<?php

class Ansprechpartner extends DataObject {
   static $singular_name = 'Ansprechpatner';
   static $plural_name = 'Ansprechpatner';

   static $db = array(
   'Anrede' => "Enum('Herr,Herr Dr.,Herr Dr. Prof,Herr Prof,Frau,Fau Dr.,Frau Dr. Prof.,Frau Prof','Herr')",
   'Vorname' => 'Varchar',
   'Nachname' => 'Varchar',
   'Mail' => 'Varchar',
   'Durchwahl' => 'Int'
   );

   static $has_one = array(
      'Stammdaten' => 'Stammdaten'
   );
   
   static $searchable_fields = array (
      'Anrede',
      'Nachname',
      'Stammdaten.Firmenname'
   );
   
   static $summary_fields = array(
      'Anrede',
      'Nachname',
      'Stammdaten.Firmenname'
   );
      
   static $field_labels = array (
      'Stammdaten.Firmenname' => 'geh&ouml;rt zu Firma'
   );
   
}

Avatar
dacar

Community Member, 168 Posts

18 August 2009 at 3:02am

I think, i have found the right way. But how can i receive the values for the from the array?

SNIPPET:

function getCMSFields() {
   $FirmenFuerAnsprechpartner = DataObject::get('Stammdaten', "", "Firmenname","", "");
   if($FirmenFuerAnsprechpartner)
foreach($FirmenFuerAnsprechpartner as $item) {
      $record = array(
         ????????
      );
   }
   debug::show($record);

FULL:

<?php

class Ansprechpartner extends DataObject {
   static $singular_name = 'Ansprechpatner';
   static $plural_name = 'Ansprechpatner';

   static $has_one = array(
         'Stammdaten' => 'Stammdaten'
      );
      
   static $db = array(
   'Anrede' => "Enum('Herr,Herr Dr.,Herr Dr. Prof,Herr Prof,Frau,Fau Dr.,Frau Dr. Prof.,Frau Prof','Herr')",
   'Vorname' => 'Varchar',
   'Nachname' => 'Varchar',
   'Mail' => 'Varchar',
   'Durchwahl' => 'Int'
   )
;   

   function getCMSFields() {
   $FirmenFuerAnsprechpartner = DataObject::get('Stammdaten', "", "Firmenname","", "");
   if($FirmenFuerAnsprechpartner)
foreach($FirmenFuerAnsprechpartner as $item) {
      $record = array(
         ????????
      );
      //$itemIDs[] = $item->ID;
   }
   debug::show($record);
      $fields = new FieldSet(
         new DropdownField('Anrede', 'Anrede', singleton('Ansprechpartner')->dbObject('Anrede')->enumValues()),
         new TextField('Vorname', 'Vorname'),
         new TextField('Nachname', 'Nachname'),
         new EmailField('Mail', 'Mail'),
         new NumericField('Durchwahl', 'Durchwahl')
         //new DropdownField('StammdatenID', 'Firmenname', $item)
      );
      // Feldname::Header::Klassenname
      //$fields->push( new TypeDropdown("ID", "Firmenname", "Stammdaten") );   
      return $fields;
   }
       
   
   static $searchable_fields = array (
      'Anrede',
      'Nachname',
      'Stammdaten.Firmenname'
   );
   
   static $summary_fields = array(
      'Anrede',
      'Nachname',
      'Stammdaten.Firmenname'
   );
      
   static $field_labels = array (
      'Stammdaten.Firmenname' => 'geh&ouml;rt zu Firma'
   );
   
}

Avatar
brokemeister

Community Member, 30 Posts

12 September 2009 at 12:21am

Checkout DataObjectSet->map().

$yourDropDown = $FirmenFuerAnsprechpartner->map();
should do the job

Avatar
dacar

Community Member, 168 Posts

14 September 2009 at 7:50am

Hi Brokemeister,

thanks for your reply. Here is the way i solved it:

   if (!is_null($firma) && $firma) {
      if ($firma->Count()) {
   
         $map = $firma->toDropDownMap(
                'ID', // the option value = value of field Question
                'Firmenname', // the option description = idem
                '- waehlen -'); // the description when nothing is selected (value = 0)
   
         $myField = new DropdownField('StammdatenID', 'Firma', $map);
         $fields->insertAfter($myField, 'Durchwahl');
      }
   }