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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObjectManager Code Examples


Go to End


84 Posts   31463 Views

Avatar
stevanovich

Community Member, 63 Posts

21 August 2009 at 8:41pm

Uncle cheese I have changed page with no luck nothing is happening with regards to sorting the datasheets I have into categories.

Here is the code may help??

All the objects are working fine just no Category sorting or title.

----------------
ResourcePage.php
---------------

class ResourcePage extends Page
{
static $has_many = array (
'Resources' => 'Resource'
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'Resources', // Source name
'Resource', // Source class
'Attachment', // File name on DataObject
array(
'Name' => 'Name',
'Description' => 'Description',
'Category' => 'Category'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);

$manager->setFilter(
'Category', // Name of field to filter
'Filter by Category', // Label for filter
singleton('Resource')->dbObject('Category')->enumValues() // Map for filter (could be $dataObject->toDropdownMap(), e.g.)
);

// If undefined, all types are allowed. Pass with or without a leading "."
$manager->setAllowedFileTypes(array('pdf','doc'));

// Label for the upload button in the popup
$manager->setBrowseButtonText("Upload (PDF or DOC only)");

// In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
$manager->setGridLabelField('Name');

// Plural form of the objects being managed. Used on the "Add" button.
// If left out, this defaults to [MyObjectName]s
$manager->setPluralTitle('Resources');

$f->addFieldToTab("Root.Content.Resources", $manager);

return $f;
}

function MyFileObjects()
{
$filter = Director::urlParam('Action');
return $filter ? $this->Resources("Category = '$filter'") : $this->Resources();
}

}
class ResourcePage_Controller extends Page_Controller

{
function category()
{
$resources = DataObject::get("Resource","Category='".$this->urlParams['ID']."'");
if($resources) {
return $this->customise(array(
'Resources' => $resources,
'CategoryTitle' => $this->urlParams['ID']
));
}
return false;
}
}

-----------------
Resource.php
----------------

class Resource extends DataObject
{
static $db = array (
'Name' => 'Text',
'Description' => 'Text',
'Category' => "Enum('CSO Storage Solutions, Storm Water Storage Solutions, Highways Applications, Surface Water & Sewage Pipelines, Water Treatment Works, Drinking Water, Renewable Energy, ATS Applications, Litespeed Applications')"
);

static $has_one = array (
'Attachment' => 'File',
'ResourcePage' => 'ResourcePage'

);

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Name'),
new TextareaField('Description'),
new DropdownField('Category','Category', singleton('Resource')->dbObject('Category')->enumValues()),
new FileIFrameField('Attachment')
);
}
}

--------------
ResourcePage.ss
--------------

<% if Resources %>
<% control Resources %> $CategoryTitle
<ul>

<li><a href="$Attachment.URL">$Name</a><br>$Description</li>

</ul>

<% end_control %>
<% end_if %>

Avatar
Richie

Community Member, 18 Posts

9 September 2009 at 2:45am

I followed your video about nested DOM but I am missing the EmployeePage.php
I assume I need this to create the page but what to add to get this running?

Thanks for the great work!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2009 at 3:45am

As long as you have EmployeePage extends Page in EmployeePage.php in your code folder, and you run a /dev/build, you should get the choice in your create dropdown menu.

Avatar
Richie

Community Member, 18 Posts

9 September 2009 at 6:40am

Edited: 09/09/2009 7:03pm

Now my EmployeePage.php looks like this:

class EmployeePage extends Page
class EmployeePage extends Page
{
	static $has_many = array (
		'Employees' => 'Employee'
	);	
	
	public function getCMSFields()
	{
		$f = parent::getCMSFields();
		$manager = new DataObjectManager(
			$this,
			'Employees',
			'Employee',
			array('Name' => 'Name', 'JobTitle' => 'Job Title'),
			'getCMSFields_forPopup'
		);
		$f->addFieldToTab("Root.Content.Employees", $manager);
		return $f;
	}

}

This only shows the DOM without the nested DOMs info (Schools/Resumes).
I'm doing something wrong, but what? Thanks again for your time.

Edit: Added function name 'getCMSFields_forPopup' inside DOM.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2009 at 6:46am

Where's your code for the Employee object?

Avatar
Richie

Community Member, 18 Posts

9 September 2009 at 6:49am

You're fast! Here it is:

class Employee extends DataObject
{
	static $db = array (
		'Name' => 'Text',
		'JobTitle' => 'Text'
	);
	
	static $has_one = array (
		'EmployeePage' => 'EmployeePage'
	);
	
	static $has_many = array (
		'Schools' => 'School',
		'Resumes' => 'Resume'
	);	
	
	public function getCMSFields_forPopup()
	{
		return new FieldSet(
			new TextField('Name'),
			new TextField('JobTitle', 'Job Title'),
			new DataObjectManager(
				$this,
				'Schools',
				'School',
				array('Title' => 'Title of School', 'Degree' => 'Degree', 'GraduationYear' => 'Graduation Year')
			),
			new FileDataObjectManager(
				$this,
				'Resumes',
				'Resume',
				'Attachment',
				array('Description' => 'Description')
			)
		);
	}
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2009 at 7:19am

You haven't passed a function name into the DOM, so it's assuming "getCMSFields()" as your popup method. Change:

public function getCMSFields_forPopup()

to

public function getCMSFields()

Avatar
Richie

Community Member, 18 Posts

9 September 2009 at 6:58pm

Edited: 09/09/2009 7:02pm

Thanks for your help. I passed the function name into the DOM (see also my edited post above).