Jump to:

2088 Posts in 1007 Topics by 455 members

E-Commerce Modules

SilverStripe Forums » E-Commerce Modules » Call to a member function toDropDownMap() on a non-object in ... Product.php

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2596 Views
  • giroro
    Avatar
    Community Member
    2 Posts

    Call to a member function toDropDownMap() on a non-object in ... Product.php Link to this post

    Dear All,

    I've tried google around but i've found no solution to creating a dropdown list with a DataObject::get()

    The background is that i'm trying to set up a store to show which district(area) is my product is located. This is what i've done.

    <?php

    class District extends DataObject {

       static $db = array(
          'Name' => 'Varchar(50)',
       );

    static $has_one = array(
    'StateID' => 'State'
    );
       
    }
       
    ?>

    also a State DataObject

    <?php

    class State extends DataObject {

       static $db = array(
          'Name' => 'Varchar(50)'
       );

    }
    ?>

    Then i want it shows up as a pull down menu in my Product, so i did this in Product.php's getCMSFields()

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

          ...
          //Database request for the object
          $myDataSet = DataObject::get("District");
          
    #      if($myDataSet){
              // This returns an array of ID => Title
              $map = $myDataSet->toDropDownMap();

             $fields->addFieldToTab("Root.Content.Main", new ListboxField(
             'District',
             'District',
             $map,
             $map[0])
             );
    #      }
          ...
       return $fields;
    }

    But DataObject::get("District"); gives me null, it seems that Product.php cannot access the District object.

    It seems my O-O concepts are really bad, can anyone give me a hand?

    Thanks in advance!

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Call to a member function toDropDownMap() on a non-object in ... Product.php Link to this post

    Well, probably you forgot to set up the other end of the relation in your Product.php, like so for instance (if you use a has_many):

       public static $has_many = array(
          'District' => 'District'
       );

    and in toDropDownMap you might want to add:

    $map = $myDataSet->toDropDownMap('ID', 'Name');

    See also:

    http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management

    and

    http://doc.silverstripe.com/doku.php?id=optionsetfield&s=todropdownmap

    Helps?

  • giroro
    Avatar
    Community Member
    2 Posts

    Re: Call to a member function toDropDownMap() on a non-object in ... Product.php Link to this post

    Thanks for the prompt reply.

    Well... i did actually declare

       static $has_one = array(
          'Image' => 'Product_Image',
          'District' => 'District'
       );

    But Product stilll can't access to DataObject::get("District").

    I'll go thru the http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management tutorial. I'll post my results if i get it right.

    2596 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.