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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObjectManager Within DataObjectManager?


Go to End


12 Posts   3089 Views

Avatar
VictorH

Community Member, 29 Posts

8 December 2009 at 9:39am

Edited: 08/12/2009 11:27am

Update: Go to page 2 and look at the first post for the final code to do this.

I have created a ProfilePage.php page that has many Photo Sets.

I also created a PhotoSet.php object that has a Title and many Photos but only managed to have the Title Field in the Pop up.

I also created a Photo.php object that has a Caption and a Photo.

What I'm having trouble is have the ability to add multiple Photos on the Photo Set pop-up of Profile Page.

Is this even possible?

Here is the code for all 3 pages.

Again I want so that when I click on "Add Photo Set" on a Profile Page to also give me the ability to add multiple images to that Photo Set not just the Photo Set title.

ProfilePage.php

<?php

class ProfilePage extends Page {
	
	static $has_many = array(
		'PhotoSets' => 'PhotoSet'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$photosetfield = new DataObjectManager(
			$this,
			'PhotoSets',
			'PhotoSet',
			array(
				'PhotoSetTitle' => 'PhotoSetTitle'
			),
			'getCMSFields_forPopup'
		);
		$photosetfield->setAddTitle('Photo Set');
		$fields->addFieldToTab('Root.Content.PhotoSets', $photosetfield);
		
		return $fields;
	}
}

class ProfilePage_Controller extends Page_Controller {}

?>

PhotoSet.php

<?php

class PhotoSet extends DataObject {
	
	static $db = array(
		'PhotoSetTitle' => 'Varchar(255)'
	);
	
	static $has_one = array(
		'ProfilePage' => 'ProfilePage'
	);
	
	static $has_many = array(
		'Photos' => 'Photo'
	);
}

?>

Photo.php

<?php

class Photo extends DataObject {
	static $db = array(
		'PhotoCaption' => 'Varchar(255)'
	);
	
	static $has_one = array(
		'Attachment' => 'File'
	);
	
	public function getCMSFields_forPopup()	{
		return new FieldSet(
			new TextField('PhotoCaption'),
			new FileIFrameField('Attachment')
		);
	}

}

?>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 9:59am

Yup. Nested DataObjectManager has been out for several months now. It works just as you'd expect.. just put it in your getCMSFields function for the DataObject.

Avatar
VictorH

Community Member, 29 Posts

8 December 2009 at 10:06am

Thanks for the reply UncleCheese.

I'm guessing this has to be done to the PhotoSet.php and this is what I'm having trouble with. I don't know how to do this :\

Can you help extend my version of the PhotoSet.php ?

I tried and this is what I got which is obviously wrong and I have no idea how to fix.

<?php

class PhotoSet extends DataObject {
	
	static $db = array(
		'PhotoSetTitle' => 'Varchar(255)'
	);
	
	static $has_one = array(
		'ProfilePage' => 'ProfilePage'
	);
	
	static $has_many = array(
		'Photos' => 'Photo'
	);
	
	public function getCMSFields_forPopup()	{
		return new FieldSet(
			return new DataObjectManager(
				$this,
				'Photos',
				'Photo',
				array(
					'PhotoCaption' => 'PhotoCaption'
				)
			);
		);
	}

}

?>

Avatar
VictorH

Community Member, 29 Posts

8 December 2009 at 10:16am

Holy moly I think I got it!

Thanks Uncle Cheese.

Here is my updated code for PhotoSet.php

<?php

class PhotoSet extends DataObject {
	
	static $db = array(
		'PhotoSetTitle' => 'Varchar(255)'
	);
	
	static $has_one = array(
		'ProfilePage' => 'ProfilePage'
	);
	
	static $has_many = array(
		'Photos' => 'Photo'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$photofield = new FileDataObjectManager(
			$this,
			'Photos',
			'Photo',
			'Attachment',
			array(
				'PhotoCaption' => 'PhotoCaption'
			),
			'getCMSFields_forPopup'
		);
		$photofield->setAddTitle('Photos');
		$fields->addFieldToTab('Root.Photos', $photofield);
		
		return $fields;
	}

}

?>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 10:18am

Cool!

Avatar
VictorH

Community Member, 29 Posts

8 December 2009 at 10:19am

Alright I thought i had it :P

I was able to get the FileDataObjectManager to appear on the pop-up but when I go to add a photo I get this warning.

[Warning] Bad class value NULL passed to ClassInfo::ancestry()

GET /localhost/admin/EditForm/field/PhotoSets/AddForm/field/Photos/upload?ctf[PhotoSets][start]=0&ctf[PhotoSets][per_page]=10&ctf[PhotoSets][showall]=0&ctf[PhotoSets][sort]=Created&ctf[PhotoSets][sort_dir]=DESC&ctf[PhotoSets][search]=&ctf[PhotoSets][filter]=

Line 155 in /Users/victorhernandez/Sites/localhost/sapphire/core/ClassInfo.php

}
147 	
148 	/**
149 	 * @todo Improve documentation
150 	 */
151 	static function ancestry($class, $onlyWithTables = false) {
152 		global $_ALL_CLASSES;
153 
154 		if(is_object($class)) $class = $class->class;
155 		else if(!is_string($class)) user_error("Bad class value " . var_export($class, true) . " passed to ClassInfo::ancestry()", E_USER_WARNING);
156 
157 		$items = $_ALL_CLASSES['parents'][$class];
158 		$items[$class] = $class;
159 		if($onlyWithTables) foreach($items as $item) {
160 			if(!DataObject::has_own_table($item)) unset($items[$item]);
161 		}

Trace

    * Bad class value NULL passed to ClassInfo::ancestry()
      Line 155 of ClassInfo.php
    * ClassInfo::ancestry()
      Line 433 of ComplexTableField.php
    * ComplexTableField->getParentIdNameRelation(Photo,,has_one)
      Line 413 of ComplexTableField.php
    * ComplexTableField->getParentIdName(,Photo)
      Line 286 of FileDataObjectManager.php
    * FileDataObjectManager->UploadForm()
      Line 227 of FileDataObjectManager.php
    * FileDataObjectManager->upload(HTTPRequest)
      Line 129 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
      Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
      Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
      Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
      Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
      Line 119 of Controller.php
    * Controller->handleRequest(HTTPRequest)
      Line 277 of Director.php
    * Director::handleRequest(HTTPRequest,Session)
      Line 121 of Director.php
    * Director::direct(/admin/EditForm/field/PhotoSets/AddForm/field/Photos/upload)
      Line 118 of main.php

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 10:20am

A couple tips -- in a DataObject subclass, you don't need to run parent::getCMSFields(). That's just for SiteTree objects. Just return a FieldSet. Also, you don't need to pass the 'getCMSFields_forPopup' to your constructor. Just name the function getCMSFields() and you should be fine without it.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 11:18am

Photo needs a has_one PhotoSet

Go to Top