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

How to call a method on a data object


Reply

14 Posts   3031 Views

Avatar
dio5

25 January 2009 at 7:16am Community Member, 501 Posts

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.

Avatar
funkygibbon

30 January 2009 at 6:51pm Community Member, 2 Posts

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

Avatar
funkygibbon

2 February 2009 at 3:31pm Community Member, 2 Posts

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.

Avatar
svinkle

4 February 2009 at 3:28am Community Member, 16 Posts

Confirmed. I've setup a vanilla rc3 test site and this works as expected. Now, upgrading from rc2 to rc3 is [url=http://silverstripe.org/upgrading-silverstripe/show/253416?showPost=253491]another thread[/url] entirely.

Many thanks for your input everyone.

Avatar
scaphis

12 March 2009 at 5:06am Community Member, 8 Posts

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

Avatar
scaphis

13 March 2009 at 9:20am Community Member, 8 Posts

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.