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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Custom selection based on an existing dataobject in another template-problem


Go to End


4 Posts   3129 Views

Avatar
SilverRay

Community Member, 167 Posts

26 January 2009 at 3:17am

OK, I have a dataobject called Product. I use this in a has_one relationship with ProductPage. Works well, I can make product pages and select a product etc.

Now, I want to make another template, let's call it Custom.php, in which I want to choose a custom selection of the existing products that are showed in the normal product pages and display various selections in a bunch of custom pages, with links to the actual product pages. So I thought of setting up a many_many using the same dataobject Product, and then be able to select the products for the custom set. A while ago I had this working using a has_one, but I cannot find my code anymore, and this time I want to use a many_many because I want to be able to have more than one custom selection. How would I do that? I tried some things with getmanymanycomponents etc. but got stuck... anyone has any suggestions? Thanks!

Avatar
dio5

Community Member, 501 Posts

27 January 2009 at 5:11am

Did you take a look at the relationships tutorial?

Should get you started pretty well.

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

Avatar
SilverRay

Community Member, 167 Posts

27 January 2009 at 5:54am

Oh yeah, I did, quite a while ago already ;)

The issue is more that I want to "switch on" products in my custom template, but those products are part of another section. Maybe I'm not clear in my question perhaps... I'll post code later today to clarify.

Avatar
SilverRay

Community Member, 167 Posts

27 January 2009 at 10:31pm

OK, I think I figured it out. Actually, thanks to dio5 indeed, because I revisited the relationship tutorial and it brought me back to the basics again, which is sometimes needed when you over-think things, as I tend to do ;)
Thanks dio5! Anyway, here's some code extracts (some irrelevant details taken out for brevity).

I have a DataObject called Product:

class Product extends DataObject {

	public static $db = array(
		'ProductNumber' => 'Text',
		'ProductName' => 'Text',
		'ProductDescription' => 'Text'
	);

	function getCMSFields_forPopup() {
		$fields = new FieldSet();
//code for fields etc.
		return $fields;
	}

Then I have a ProductPage:

class ProductPage extends Page {

	public static $db = array(
	"SomeSwitch" => "Boolean"
	);

	public static $has_one = array(
		'Image01' => 'Image',
		'MyProduct' => 'Product'
	);

	public static $many_many = array(
		'ProductColors' => 'ProductColor'
	);

	public static $belongs_many_many = array(
		'CustomPages' => 'CustomPage'
	);

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

		$productsTablefield = new HasOneComplexTableField(
			$this,
			'MyProduct',
			'Product',
			array(
				'ProductNumber' => 'Number',
				'ProductName' => 'Name',
				'ProductDescription' => 'Description'
			),
			'getCMSFields_forPopup'
		);
		$productsTablefield->setParentClass('ProductPage');
		$productsTablefield->setOneToOne();
		$productsTablefield->setPageSize(50);
		$productsTablefield->setAddTitle( 'a Product' );

		$colorsTablefield = new ManyManyComplexTableField(
			$this,
			'ProductColors',
			'ProductColor',
			array(
				'ColorName' => 'Color name',
				'ColorCode' => 'Color code'
			),
			'getCMSFields_forPopup'
		);
		$colorsTablefield->setParentClass('ProductPage');
		$colorsTablefield->setPageSize(50);
		$colorsTablefield->setAddTitle( 'a Color' );

		$fields->addFieldToTab( 'Root.Content.Images', new CheckboxField("SomeSwitch", "Test switch."));
		$fields->addFieldToTab('Root.Content.Images', new ImageField('Image01', 'Image 1.'));

		$fields->addFieldToTab( 'Root.Content.Products', $productsTablefield );
		$fields->addFieldToTab( 'Root.Content.Colors', $colorsTablefield );

		return $fields;
	}

}

class ProductPage_Controller extends Page_Controller {

}

This works of course, I can make product pages, select an associated product in the table, have it rendered in the template etc., the usual. Then I want to make a custom page with a custom selection of products that link to their respective product pages:

class CustomPage extends Page {

	public static $db = array(
	);

	public static $has_one = array(
	);

	public static $many_many = array(
		'ProductPages' => 'ProductPage'
	);

	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$customsetTablefield = new ManyManyComplexTableField(
			$this,
			'ProductPages',
			'ProductPage',
			array(
				'MenuTitle' => 'Name'
			),
			'getCMSFields_forPopup'
		);
		$customsetTablefield->setParentClass('CustomPage');
		$customsetTablefield->setPageSize(50);
		$customsetTablefield->setPermissions(array());

		$fields->addFieldToTab( 'Root.Content.ProductsToShow', $customsetTablefield );
		return $fields;
	}

}

class CustomPage_Controller extends Page_Controller {
}

Now I can make various custom pages, select a bunch of products per custom page, render it with a ProductPages control block including links to the original product pages, etc. That was all there is to it. First I was thinking of using all kinds of functions with DataObject::get statements etc. but there's no need. The power of an object oriented framework indeed!

If you see anything wrong, please let me know. The only thing I'm stuck at is how to show Product properties like ProductName, ProductNumber etc. in the productsTablefield...

Thanks!