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

How do I set up this relationship / data object?


Go to End


3 Posts   1794 Views

Avatar
LinseyM

Community Member, 99 Posts

5 May 2010 at 10:44pm

Hi there guys,

I've been reading through the Job Postings tutorial in the SS book, but I've tied myself in knots, so I thought I would just ask instead, as I am beyond confused now. (This vaguely relates to a previous post, but am starting over as the client changed their mind on the last part of the project anyway!)

OK, I want to created a data object. Let's call it "LinkedPDF"

The basic idea is that from any "service page"* on the website CMS I have a tab called "Related PDFs". (*these pages all use the template and page type "ServicePage")

In this "Related PDFs" tab I will show a ComplexTableField. This will allow users to add links to related PDF files to the web page (plus some notes / text / graphic). The plan is that they can add as many Related PDF items as they want, so no point hard-coding 5 links in the static $db for example. Also, I want to do it this way rather than asking them just to create web links on the page as they want it laid out / formatted in a way that would be too tricky for them to achieve in the CMS.

Anyway, I followed the Job Posting tutorial and got the ComplexTableField working. Thought all was well, and added a few linked case studies for "Service Page A", but then i realised that these case studies now appeared linked to ALL Pages that used the "ServicePage" template / page type! DOH!

So, i kinda know where I've gone wrong... the data objects are linked to the page type and not the actual page i created them from.

It's not practical to create a new "Holder" for each service page - there are too many - so how can I fix this?

Hope I've got all the techie terminology correct re dataObjects, relationships etc...!

All answers on a postcard please - any help much appreciated.

Thanks,

Linsey :)

Avatar
MarcusDalgren

Community Member, 288 Posts

6 May 2010 at 3:44am

Hi Lindsey!

First of all, I would really really recommend that you get the DataObject Manager. If you're running on 2.4 then get the trunk version, otherwise get the 2.3 branch. You'll also need the SWFUpload module (can be found here).

DataObject Manager is essentially a very much improved version of ComplexTableField so you use that instead of ComplexTableField.
There is a special version of DataObject Manager called FileDataObjectManager and it's perfect for the scenario that you are describing (if I'm understanding you correctly).

When it comes to the ServicePage and the LinkedPDF, make sure that ServicePage $has_many LinkedPDF and LinkedPDF $has_one ServicePage so that the relationship is setup properly.

ServicePage

public static $has_many = array(
	'LinkedPDFs' 	=> 'LinkedPDF'
);

LinkedPDF

static $has_one = array (
	'Attachment' => 'File',
	'ServicePage' => 'ServicePage'
);

Now as you might have noticed the LinkedPDF is not a file in itself but a DataObject linked to a file (this might be the way you've done it already, I'm not sure). You can have whatever other fields you want on the LinkedPDF object according to your needs.

Last but not least you add the FileDataObjectManager in the getCMSFields() method like so

ServicePage

public function getCMSFields() {
	$fields = parent::getCMSFields();
	$manager = new FileDataObjectManager(
		$this, // Controller
		'LinkedPDFs', // Source name
		'LinkedPDF', // Source class
		'Attachment', // File name on DataObject
		array(), // Headings
	);
       $fields->addFieldToTab("Root.Content.LinkedPDFs", $manager);
    
	return $fields;
}

With this code you should be able to start linking your files to your ServicePages. I've also included the basics from the code I use when my clients need to have the ability to link an arbitrary number of files to any page in the system.

Kindly,
Marcus

Avatar
LinseyM

Community Member, 99 Posts

6 May 2010 at 3:51am

Hi marcus,

Thanks so much for this detailed reply.

I'll have a look at the version requirements and read up on the DataObjectManager.

Will give it all a try tomorrow morning when I have a clear head - its too late in the day for anything too complicated now!

Many many thanks!

Linsey