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, biapar, Willr, Ingo, swaiba

DataObject PAge


Go to End
Reply


15 Posts   5094 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 July 2009 at 1:01am

What about

$this->PersonenPages()->First()->URLSegment

Avatar
theAlien

Community Member, 131 Posts

16 July 2009 at 1:07am

Whoah! You're fast!

However, no luck :(

[Notice] Trying to get property of non-object

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 July 2009 at 1:41am

Post your code for the Page and the DataObject.

Avatar
theAlien

Community Member, 131 Posts

16 July 2009 at 1:52am

Code of the Page:

<?php

class PersonenPage extends Page {
	static $many_many = array(
		'PersonenObjectStandaard' => 'PersonenObject',
		'PersonenObjectUitgelicht' => 'PersonenObject'
	);
	public function getCMSFields() {
		$PersonenObjectStandaard = new ManyManyDataObjectManager (
			$this,
			'PersonenObjectStandaard',
			'PersonenObject',
			array(
				'Name'		=> 'Naam',
				'Function'	=> 'Functie',
				'Notes'		=> 'Opmerkingen'
			),
			'getCMSFields_forPopup','',
			'Name ASC'
		);
		$PersonenObjectStandaard->setAddTitle('Personen');
		$PersonenObjectStandaard->setSingleTitle('Persoon');
		
		$PersonenObjectUitgelicht = new ManyManyDataObjectManager (
			$this,
			'PersonenObjectUitgelicht',
			'PersonenObject',
			array(
				'Name'		=> 'Naam',
				'Function'	=> 'Functie',
				'Notes'		=> 'Opmerkingen'
			),
			'getCMSFields_forPopup','',
			'Name ASC'
		);
		$PersonenObjectUitgelicht->setAddTitle('Personen');
		$PersonenObjectUitgelicht->setSingleTitle('Persoon');

		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab("Root.Content.Personen", new TabSet( "Personen",
			new tab('Standaard',new LiteralField('LiF1','<p>Kies hier welke personen weergegeven moeten worden.</p>'), $PersonenObjectStandaard),
			new tab('Uitgelicht',new LiteralField('LiF2',"<p>Kies hier enkele personen die met extra nadruk weergegeven moeten worden (deselecteer deze op het tabblad 'Standaard').</p>"),$PersonenObjectUitgelicht)
		));
		return $fields;
		}
}

class PersonenPage_Controller extends Page_Controller {
		function view($id = null) {
		if($id == null)
			$id = $this->urlParams['ID'];
		if($id && is_numeric($id))
			return DataObject::get_by_id("PersonenObject", $id);
	}
}
?>

Code of the DataObject:

<?php

class PersonenObject extends DataObject{

static $db = array(
	'Name'		=> 'Varchar(50)',
	'Function'	=> 'Varchar(50)',
	'Address'	=> 'HTMLText',
	'Phone'		=> 'Varchar(50)',
	'Email'		=> 'Varchar(50)',
	'URL'		=> 'Text',
	'Bio'		=> 'HTMLText',
	'Notes'		=> 'Text',
);

static $has_one = array(
	'Image'					=> 'Image',
);
static $belongs_many_many = array(
	'PersonenPage'			=> 'PersonenPage'
);

function Thumbnail(){
	$Image = $this->Image();
	if ( $Image ) {
		return $Image->CMSThumbnail();
	} else {
		return null;
	}
}

function getCMSFields_forPopup() {
	return new FieldSet(
		new TextField('Name','Naam'),
		new TextField('Function','Functie'),
		new TextField('Address','Adres'),
		new TextField('Phone','Telefoon'),
		new TextField('Email','E-mail (toont een link naar een contactformulier)'),
		new TextField('URL','Website'),
		new SimpleHTMLEditorField('Bio','Biografie',array(
				'css'				=> 'mysite/css/jquery.wysiwyg.css',
				'insertOrderedList'	=> false,
				'insertImage'		=> false,
				'justifyLeft'		=> false,
				'justifyRight'		=> false,
				'justifyCenter'		=> false,
				'cut'				=> true,
				'copy'				=> true,
				'paste'				=> true,
				'html'				=> true)
		),
		new myImageField('Image','Portret'),
		new TextareaField('Notes','Opmerkingen (worden niet weergegeven op de website)')
	);
}

function Link() {
	return $this->PersonenPage()->First()->URLSegment . "view/" . $this->ID;
}

}
?>

Avatar
logix812

Community Member, 4 Posts

28 July 2009 at 10:08am

Edited: 28/07/2009 10:10am

I got this working...

So I have a holder:
http://pastebin.com/f5106bdce

I create a page in the CMS for that holder.

Now I can:
http://mysite.com/holder/<any text>/12
http://mysite.com/holder/<any text>/100

eg:
http://mysite.com/holder/view/12
http://mysite.com/holder/view/100

http://mysite.com/holder/silverstripe/12
http://mysite.com/holder/silverstripe/100

Note the use us static $song in the function song().
If you don't do that then you will hit the DB for every $song.property in your template.

I did not modify the Song object at all, eg: Overriding Link()

Avatar
bartvanirsel

Community Member, 96 Posts

30 July 2009 at 12:31am

Hi!

I took a look at this and i have a solution which takes the current controller into the url using the SSViewer object.

I dont know if it is the ideal way but it is a solution i think. Maybe some of the Silverstripe devs can take a look at this?

My code:

PersonenPage.php

<?php 

class PersonenPage extends Page {
   static $many_many = array(
      'PersonenObjectStandaard' => 'PersonenObject',
      'PersonenObjectUitgelicht' => 'PersonenObject'
   );
   public function getCMSFields() {
      $PersonenObjectStandaard = new ManyManyDataObjectManager (
         $this,
         'PersonenObjectStandaard',
         'PersonenObject',
         array(
            'Name'      => 'Naam',
            'Function'   => 'Functie',
            'Notes'      => 'Opmerkingen'
         ),
         'getCMSFields_forPopup','',
         'Name ASC'
      );
      $PersonenObjectStandaard->setAddTitle('Personen');
      $PersonenObjectStandaard->setSingleTitle('Persoon');
      
      $PersonenObjectUitgelicht = new ManyManyDataObjectManager (
         $this,
         'PersonenObjectUitgelicht',
         'PersonenObject',
         array(
            'Name'      => 'Naam',
            'Function'   => 'Functie',
            'Notes'      => 'Opmerkingen'
         ),
         'getCMSFields_forPopup','',
         'Name ASC'
      );
      $PersonenObjectUitgelicht->setAddTitle('Personen');
      $PersonenObjectUitgelicht->setSingleTitle('Persoon');

      $fields = parent::getCMSFields();
      
      $fields->addFieldToTab("Root.Content.Personen", new TabSet( "Personen",
         new tab('Standaard',new LiteralField('LiF1','<p>Kies hier welke personen weergegeven moeten worden.</p>'), $PersonenObjectStandaard),
         new tab('Uitgelicht',new LiteralField('LiF2',"<p>Kies hier enkele personen die met extra nadruk weergegeven moeten worden (deselecteer deze op het tabblad 'Standaard').</p>"),$PersonenObjectUitgelicht)
      ));
      return $fields;
      }
}

class PersonenPage_Controller extends Page_Controller {

	public function PersonenList() {
		$records = DataObject::get('PersonenObject');	
		return $records;
	}	

	function view($id = null) {
		
    	$params = Director::urlParams();
    	$id = (int)$params['ID'];
      
    	$object = DataObject::get_by_id('PersonenObject',$id);
    
    	if($object) {
    		return $this
    			->customise(array('DisplayPersoon' => $object))
    			->renderWith(array('DisplayPersoon', 'Page'));
    	} else {
    		Director::redirect(Director::absoluteBaseURL() . 'not-found');
    	}
   } 	
	
}
?>

PersonenObject

<?php

class PersonenObject extends DataObject{

	static $db = array(
   		'Name'      => 'Varchar(50)',
   		'Function'   => 'Varchar(50)',
   		'Address'   => 'HTMLText',
   		'Phone'      => 'Varchar(50)',
   		'Email'      => 'Varchar(50)',
   		'URL'      => 'Text',
   		'Bio'      => 'HTMLText',
   		'Notes'      => 'Text',
	);

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

	static $belongs_many_many = array(
   		'PersonenPage'         => 'PersonenPage'
	);

	function Thumbnail(){
   		$Image = $this->Image();
   		if ( $Image ) {
      		return $Image->CMSThumbnail();
   		} else {
      		return null;
   		}
	}

	function getCMSFields_forPopup() {
   		return new FieldSet(
      		new TextField('Name','Naam'),
      		new TextField('Function','Functie'),
      		new TextField('Address','Adres'),
      		new TextField('Phone','Telefoon'),
      		new TextField('Email','E-mail (toont een link naar een contactformulier)'),
      		new TextField('URL','Website'),
      		new SimpleHTMLEditorField('Bio','Biografie',array(
            		'css'            => 'mysite/css/jquery.wysiwyg.css',
            		'insertOrderedList'   => false,
            		'insertImage'      => false,
            		'justifyLeft'      => false,
            		'justifyRight'      => false,
            		'justifyCenter'      => false,
            		'cut'            => true,
            		'copy'            => true,
            		'paste'            => true,
            		'html'            => true)
      		),
      		new ImageField('Image','Portret'),
      		new TextareaField('Notes','Opmerkingen (worden niet weergegeven op de website)')
   		);
	}

	function Link() {
   		return Director::absoluteBaseURL() . SSViewer::topLevel()->URLSegment . "/view/" . $this->ID; 
	}

}
?>

Avatar
Stefdv

Community Member, 110 Posts

15 November 2009 at 7:52am

Edited: 07/01/2010 7:43am

Deleted by poster

Go to Top