Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » modelAdmin -> receive data for Dropdown

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

Page: 1
Go to End
Author Topic: 2312 Views
  • dacar
    Avatar
    Community Member
    168 Posts

    modelAdmin -> receive data for Dropdown Link to this post

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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: modelAdmin -> receive data for Dropdown Link to this post

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

  • brokemeister
    Avatar
    Community Member
    30 Posts

    Re: modelAdmin -> receive data for Dropdown Link to this post

    Checkout DataObjectSet->map().

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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: modelAdmin -> receive data for Dropdown Link to this post

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

    2312 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.