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.

Data Model Questions /

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

DataObjectDecorator


Go to End
Reply


2 Posts   659 Views

Avatar
azt3k

Community Member, 9 Posts

19 June 2012 at 11:57am

Has anyone managed to develop an extension that uses relationships?

I have tried to set up a dataobject decorator to allow comments to placed on multiple DataObjects. The result is something like this:

class Commentable extends DataObjectDecorator{

	public function extraStatics() {
		return array(
			'has_many' 	=> array(
				'Comments'		=> 'Comment'
			)
		);
	}

	public function updateCMSFields($fields) {
		
		if(get_class($fields->fieldByName('Root.Content')) == 'TabSet'){

			$fields->addFieldsToTab('Root.Content.Comments', $this->getCommentFields());

		}elseif(get_class($fields->fieldByName('Root')) == 'TabSet'){

			$fields->addFieldsToTab('Root.Comments', $this->getCommentFields());

		}elseif(get_class($fields) == 'FieldSet'){
			foreach($this->getCommentFields() as $f){
				$fields->push($f);
			}
		}
		
	}

	protected function getCommentFields() {

		$fields = array();

		// Create complex table field for managing attached comments
		$CommentsField = new HasManyDataObjectManager(
			$this->owner,
			'Comments',
			'Comment',
			array(
				'Created'				=> 'Date Posted',
				'Comment'		  	=> 'Comment'
			),
			'getCMSFields_forPopup'
		);
		$CommentsField->setParentClass('DataObject');

		$fields[] = $CommentsField;

		return $fields;
	}

	// other code here

}

with an associated DataObject here:

class Comment extends DataObject{
	
	public static $db = array(
		'Comment'	=> 'HTMLText',
		'IsDeleted'	=> 'Boolean'
	);

	public static $has_one = array(
		'OwnerObject'	=> 'DataObject' // This line is fishy...
	);

	// other code here

}

Then add the extension to the Page class for argument's sake

Object::add_extension('Page', 'Commentable');

So the environment builder runs, tables get made - looks fine until I try to save a page - it throws an error - it seems like the dataobjectmanager / complex table get confused.

The error is as follows:

Error at \sapphire\core\Object.php line 724: Uncaught Exception: Object->__call(): the method 'setbyidlist' does not exist on 'DataObjectSet' (http://my.domain.tld/admin/EditForm)

Avatar
Willr

Forum Moderator, 5521 Posts

19 June 2012 at 6:13pm

As an aside, there is a comments module which works on DataObjects (https://github.com/silverstripe/silverstripe-comments).