Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » using DataObject::get() to fill dropdown in getCMSFields_forPopup()

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

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

Page: 1 2
Go to End
Author Topic: 8035 Views
  • RobertM
    Avatar
    Community Member
    26 Posts

    using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

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

    }

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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.

  • RobertM
    Avatar
    Community Member
    26 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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

  • RobertM
    Avatar
    Community Member
    26 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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

  • RobertM
    Avatar
    Community Member
    26 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

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

    }

    ?>

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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

  • RobertM
    Avatar
    Community Member
    26 Posts

    Re: using DataObject::get() to fill dropdown in getCMSFields_forPopup() Link to this post

    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>

    8035 Views
Page: 1 2
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.