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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

DataObjects Per Page Instance


Go to End


14 Posts   4465 Views

Avatar
Lazarus404

Community Member, 72 Posts

14 March 2011 at 11:24pm

Hey guys,

I have a DataObject :-

class PanelContent extends DataObject
{
	public static $TopID = 1;
	
	static $db = array(
		'Title' => 'Varchar',
		'Group' => 'Varchar',
		'Content' => 'HTMLText'
	);
 
	static $has_one = array(
		'Image' => 'Image',
		'Page' => 'Page'
	);

	function getCMSFields_forPopup()
	{
		$fields = new FieldSet();
		$fields->push( new TextField( 'Title' ) );
		$fields->push( new TextField( 'Group' ) );
		$fields->push( new SimpleHTMLEditorField( 'Content' ) );
		$fields->push( new ImageField( 'Image', 'Header Image', null, null, null, 'Uploads' ) );
		return $fields;
	}
}

This object acts as a panel in a given template

My page object then links them like this :-

public static $has_many = array(
	"PanelItems" => "PanelContent"
);

This works great for one page, but the problem is, if I create more instances of this page, they "share" those objects. I need the objects to be bespoke for each page. How can I do this?

Thanks loads,
Lee

Avatar
Lazarus404

Community Member, 72 Posts

15 March 2011 at 2:59am

Anyone?

Avatar
Gelert

Community Member, 14 Posts

15 March 2011 at 3:06am

Try following this tutorial:
http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/

Or another similar one on that site.

I've used them to solve what you're having difficulty with.

I think you need a PageHolder to hold the Data Objects from looking at your code.

Good luck!

Avatar
Lazarus404

Community Member, 72 Posts

15 March 2011 at 4:22am

Edited: 15/03/2011 4:23am

Thank you, but this doesn't cover my predicament. I need to have it so no dataobjects from other pages (with the same template) are visible in each other page using the same template. So, in my circumstance, I have a bio's page and a clients page. Both pages use the same dataobject types, and the same page template, but both should have only their own data. They shouldn't cross pollute.

Thanks,
Lee

Avatar
Gelert

Community Member, 14 Posts

15 March 2011 at 4:39am

Edited: 15/03/2011 4:39am

I see. You need to filter your Dataobjects on something which is unique to each page.

So add a Enumerated data item called "DisplayOn" containing "bio" and "clients" and assign each Data Object a DisplayOn.

"DisplayOn" => "Enum('bio, clients')"

Then in your page controller where you want to display create two functions, one to filter one way and one to filter the other. Then you won't get Data Objects appearing on the wrong page.

<?php 

	function BioObjects() {
		// set filters
		$filterSQL=" 1=1 ";
		$filterSQL .= " AND DisplayOn = 'bio'";			
		
		$BioObjects = DataObject::get(
			"PanelContent",
			$filterSQL, // filter
			null, // sort
			null, // join
			null // limit
		);
		
		return $BioObjects;
		exit;
	} // end function


 ?>
 
/// Template

	<% if BioObjects %>
	<% control BioObjects %>
			<h1>$Title</h1>
	<% end_control %>
	<% end_if %>


<?php 

	function ClientObjects() {
		// set filters
		$filterSQL=" 1=1 ";
		$filterSQL .= " AND DisplayOn = 'clients'";			
		
		$ClientObjects = DataObject::get(
			"PanelContent",
			$filterSQL, // filter
			null, // sort
			null, // join
			null // limit
		);
		
		return $ClientObjects;
		exit;
	} // end function


 ?>
 
/// Template 2

	<% if ClientObjects %>
	<% control ClientObjects %>
			<h1>$Title</h1>
	<% end_control %>
	<% end_if %>

You can be more sophisticated and use a ?filter=bio and ?filter=clients and grab that from the URL if you need. But I'd just make two PageTypes and hard-wire the filter in this situation.

Best
Aled

Avatar
martimiz

Forum Moderator, 1391 Posts

15 March 2011 at 6:34am

Edited: 15/03/2011 6:37am

The way you set this up looks OK. Your DataObject will have a PageID field, that should hold the link to the correct page (it will be created based on the has_one relation to the page you defined), but all depends on how you add your DataObjects to an individual Page.

Are you using something like a ComplexTableField or even a DataObjectManager on your Page? In that case the PageID field should be set automatically, you could check your database table ..

If the PageID is set correctly, then the simplest way of displaying one or more DataObjects on a given page is something like this in your Page.ss template:

<div id='somePanel'>
<% control PanelItems %>
$Title <br />
$Group <br />
$Content
<% end_control %>
</div>

There are lots of different setups thinkable, but you'll always need the PageID present in your DataObjects.

Avatar
Lazarus404

Community Member, 72 Posts

15 March 2011 at 6:52am

Hi Martimiz,

I figured there must be an easier way because of that link. In my PanelPage class, I'm setting up the CMS fields like this:

    function getCMSFields() {
		$fields = parent::getCMSFields();
		$contentTablefield = new HasManyDataObjectManager(
			$this,
			'PanelItems',
			'PanelContent',
			array(
				'Title' => 'Title',
				'Group' => 'Group Name',
				'Content' => 'Content'
			),
			'getCMSFields_forPopup'
		);
		$contentTablefield->setAddTitle( 'Content Items' );

		$fields->addFieldToTab( 'Root.Content.ContentPanels', $contentTablefield );

		return $fields;
    }

In the CMS, both pages show all PanelItems, where I really only want to show the items specific to the page.

Thanks for your time.

Lee

PS @Gelert, thanks for your time... I really appreciate that, too :)

Avatar
Lazarus404

Community Member, 72 Posts

15 March 2011 at 6:58am

Oh, and yes, the PageID is set for these objects :)

Thanks,
Lee

Go to Top