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 /

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

How to call a method on a data object


Go to End
Reply


14 Posts   3443 Views

Avatar
dio5

Community Member, 501 Posts

25 January 2009 at 7:16am

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

Community Member, 2 Posts

30 January 2009 at 6:51pm

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

Community Member, 2 Posts

2 February 2009 at 3:31pm

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

Community Member, 16 Posts

4 February 2009 at 3:28am

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.

Avatar
scaphis

Community Member, 8 Posts

12 March 2009 at 5:06am

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

Community Member, 8 Posts

13 March 2009 at 9:20am

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.

Go to Top