Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to call a method on a data object

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

Page: 1 2
Go to End
Author Topic: 2967 Views
  • dio5
    Avatar
    Community Member
    501 Posts

    Re: How to call a method on a data object Link to this post

    Hi,

    sorry for the late reply.

    I have this on my holderpage:

    $price_groups = DataObject::get("PriceGroup");
             $map = $price_groups ? $price_groups->toDropDownMap("ID", "Value") : array("No Price Groups Available");
             
             $price_table = new ComplexTableField(
                $this,
                "Prices",
                "Price",
                array(
                   "PriceGroupTitle" => "Group",
                   "Value" => "Value",
                   "Label" => "Label",
                   "Price" => "Price"
                ),
                new FieldSet(
                   new DropdownField("PriceGroupID", "PriceGroup", $map),
                   new TextField("Value"),
                   new TextField("Label"),
                   new TextField("Price")
                ),
                "",
                "PriceGroupID ASC, Value ASC, Price ASC"
             );
             
             $price_table->setPageSize(50);
             
             $fields->addFieldToTab('Root.Content.PriceTable', $price_table);

    And this in my Price class:

    <?php

       class Price extends DataObject{
          
          static $db = array(
             "Value" => "Varchar(200)",
             "Label" => "Varchar",
             "Price" => "Varchar"
          );
          
          static $has_one = array(
             "PricelistPage" => "PricelistPage",
             "PriceGroup" => "PriceGroup"
          );
          
          function getPriceGroupTitle()
          {
             $g = DataObject::get_by_id("PriceGroup", $this->PriceGroupID);
             return $g ? $g->Value : "No group selected";;         
          }   
       }

    Hope this helps.

  • funkygibbon
    Avatar
    Community Member
    2 Posts

    Re: How to call a method on a data object Link to this post

    I seem to recall having similar difficulty getting functions to work as fields with 'exotic' CTFs - HasMany, HasOne or ManyMany, whereas it works as intended in ComplexTableFields.

    testing...
    Yep, confirmed. Works fine on CTF, not on HasOne. This is using branches/2.3 rv70871.

    This code works:

    function Chickens() {
          return "Bkawk!";
    }

    function getCMSFields() {
          $f=parent::getCMSFields();
          
          $f->addFieldToTab("Root.Content.Page",new ComplexTableField (
             $this,
             "TestPages",
             "TestPage",
             array(
                "ID"=>"ID",
                "Title"=>"Title",
                "Chickens"=>"Chickens"
             ),
             'getCMSFields_forPopup'
          ));

    This code does not:

          $f->addFieldToTab("Root.Content.Page",new HasOneComplexTableField (
             $this,
             "TestPage",
             "TestPage",
             array(
                "ID"=>"ID",
                "Title"=>"Title",
                "Chickens"=>"Chickens"
             ),
             'getCMSFields_forPopup'
          ));

    Only difference is the HasOneComplex... instead of Complex...

  • funkygibbon
    Avatar
    Community Member
    2 Posts

    Re: How to call a method on a data object Link to this post

    Just a note to say this now appears to be working as intended. 2.3/Branch @ r71121 or later you can use functions as fields like the example i gave above in relation CTFs.

  • svinkle
    Avatar
    Community Member
    16 Posts

    Re: How to call a method on a data object Link to this post

    Confirmed. I've setup a vanilla rc3 test site and this works as expected. Now, upgrading from rc2 to rc3 is another thread entirely.

    Many thanks for your input everyone.

  • scaphis
    Avatar
    Community Member
    8 Posts

    Re: How to call a method on a data object Link to this post

    I think I have the price class in the rightr spot on products.php, but what is a holderpage and where do I put this code?

    $price_groups = DataObject::get("PriceGroup");
    $map = $price_groups ? $price_groups->toDropDownMap("ID", "Value") : array("No Price Groups Available");

    $price_table = new ComplexTableField(
    $this,
    "Prices",
    "Price",
    array(
    "PriceGroupTitle" => "Group",
    "Value" => "Value",
    "Label" => "Label",
    "Price" => "Price"
    ),
    new FieldSet(
    new DropdownField("PriceGroupID", "PriceGroup", $map),
    new TextField("Value"),
    new TextField("Label"),
    new TextField("Price")
    ),
    "",
    "PriceGroupID ASC, Value ASC, Price ASC"
    );

    $price_table->setPageSize(50);

    $fields->addFieldToTab('Root.Content.PriceTable', $price_table);

    Thanks,

    Trying to have quantity price breaks

  • scaphis
    Avatar
    Community Member
    8 Posts

    Re: How to call a method on a data object Link to this post

    Ok I realize that people are working on product attributes for the ecommerce module.

    I will need to have patience since I have very little php experience.

    Would be awesome to have a quantity price breaks module like OsCommerce.

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