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

Portfolio Page with $has_one AND $has_many


Go to End


4 Posts   3952 Views

Avatar
pklopp

Community Member, 4 Posts

31 March 2010 at 6:26am

Maybe someone can help me:

I need to create a portfoliopage. The portfolio Page has a $has_many(Projects). Every Project should have 1 to many images.

But when I create ComplexTableField for Projects, everything is good, but then I need to attach the images with another ComplexTableField inside the Popup-Window and it doesnt show me buttons to add or edit the files.

Is there any better way to do it?

Regards,

PKLOPP

Avatar
Willr

Forum Moderator, 5523 Posts

31 March 2010 at 7:37pm

I don't believe Complex Table Fields inside other CTFs work very well, if at all. I guess one way you could get this to work is use model admin to manage projects. Then in the edit interface it will have a separate tab for the managing images. Then to attach projects to a page you could use a HasManyComplexTableField. But still you would need to use model admin to manage images.

Another option would be to have 2 CTFs on your page. One for projects and one for images. The images tab would filter the list (see docs for adding filters) so that it only showed the images for projects which are attached to the same page.

Avatar
pklopp

Community Member, 4 Posts

31 March 2010 at 9:53pm

thanks a lot!

At the Moment I'm thinking about making every portfoliopage to a "real" page and then adding there with $has_many images... :)

Avatar
patjnr

Community Member, 102 Posts

2 April 2010 at 3:50am

Hi

try this

set you has one relationship in ProjectPage.php

class ProjectPage extends Page {
	
	static $has_one = array(
		'PortfolioPage' => 'PortfolioPage'
	);
..............

PortfolioPage.php

	static $has_many = array (

		'Projects' => 'ProjectPage ',
	);
	public function getCMSFields()	{
		$f = parent::getCMSFields();
		
		$tablefield = new HasManyComplexTableField(
		 $this,
		 'Projects',
		 'ProjectPage ',
		 array(
			'Title' => 'Title',
		 ),
		 'getCMSFields_forPopup'
		);
		$tablefield->setPermissions(array("show")); 
		
		$f->addFieldToTab( 'Root.Content.Projects', $tablefield );
	
		return $f;
	}
	
	public function onBeforeWrite() 
	{
		$beforePage = DataObject::get("ProjectPage ", 'PortfolioPage="1"' ');
		if($beforePage) 
		{
			foreach($beforePage as $page) {
				$page->writeToStage('Stage');
				$page->publish('Stage', 'Live');
				$page->flushCache();
			}
		}
		parent::onBeforeWrite();
	}

tutorial here http://doc.silverstripe.org/doku.php?id=hasmanycomplextablefield

now your structure could be like this.

ProjectHolder
ProjectPage(1)
ProjectPage(2)
ProjectPage(3)
ProjectPage(x)
...

PortfolioHolder
PortfolioPage

in portfolio page you will have a HasManyComplexTableField showing all your project which are of page Type ProjectPage
In an portfolio just tick the projects you want to associate with that Portfolio you are in.
as for the image in you Project page under $has_many put this line


	static $has_many = array (
		'Images' => 'Resource',
	);
	protected static $_cached_pages = null;
	
	public function getCMSFields()	{
		$f = parent::getCMSFields();
		$fileMan = new ImageDataObjectManager(
		$this,
			'Images',
			'Resource',
			'Attachment', 
		array(
		'Name' => 'Name', 
		'Caption' => 'Caption',
		),
		'getCMSFields_forPopup'
		);
		$fileMan->setAllowedFileTypes(array('jpg'));
		$fileMan->setGridLabelField('Name');
		$fileMan->setAddTitle('Images');
		$f->addFieldToTab("Root.Content.Images",  new HeaderField(
			$title = "Images",
			$headingLevel = "2"
		));
		$f->addFieldToTab("Root.Content.Images", $fileMan);
		
	
		
	
		 return $f;
	}

Resource.php

<?php class Resource extends DataObject
{
	static $db = array (
		'Name' => 'Text',
		'Caption' => 'Text',
	);

	static $has_one = array (
		'Attachment' => 'Image',
		'ProjectPage ' => 'ProjectPage ',
	);

	public function getCMSFields_forPopup()
	{
		return new FieldSet(
		new TextField('Name'),
		new TextareaField('Caption'),
		new FileIFrameField('Attachment' , 'Image .. (Please don&acute;t upload an image which is more than 1mb)')
		);
	}

}
?>

for the images tutorials here
http://doc.silverstripe.org/doku.php?id=modules:dataobjectmanager&s=resource%20php