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.

Archive /

Our old forums are still available as a read-only archive.

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

How to show image name or url in ManyManyComplexTableField?


Go to End


3 Posts   2160 Views

Avatar
SilverRay

Community Member, 167 Posts

14 August 2008 at 7:19pm

OK, I have the following dataobject:

<?php

class TestObject extends DataObject {

	static $db = array(
	);

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

	static $belongs_many_many = array(
		'TestPages' => 'TestPage'
	);

	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push( new ImageField( 'FileName', 'ColorImage' ) );
		return $fields;
	}

}
?>

And then I have the following code in TestPage.php:

	static $many_many = array(
		'TestObjects' => 'TestObject'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$testTablefield = new ManyManyComplexTableField(
			$this,
			'TestObjects',
			'TestObject',
			array(
				'FileNameID' => 'File'
			),
			'getCMSFields_forPopup'
		);
		$testTablefield->setPageSize(100);
		$testTablefield->setAddTitle( 'a TestObject' );
		$fields->addFieldToTab( 'Root.Content.TestObjects', $testTablefield );
		return $fields;
	}

Everything works, saves, etc. But: in the many-many table in the TestObjects tab, the FileName shows up as an ID. If I remove the "ID" from FileName, I get a server error. How can I show the name of the file, or the url, in the many-many table?

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

14 August 2008 at 10:48pm

ok thats because the field on your TestObject isnt 'FileName' as you defined in your has_one. Rather it creates a seperate table called FileName and then a column in your testobject called 'FileNameID' so from your TestObject the only field on FileName it knows about is is FileNameID.

Now the most straight forward way to get this to work is to define a method on TestObject which returns the File as a dataobject. So on TestObject you can add something like

function getImageFileName() {
if($this->FileNameID) {
$file = DataObject::get_by_id("Image", $this->FileNameID); // returns the file object
return ($file) ? $file->Filename : "No File Name";
}
return "No File Name";
}

then in your table field you can call ImagesFileName ?.

 $testTablefield = new ManyManyComplexTableField( 
         $this, 
         'TestObjects', 
         'TestObject', 
         array( 
            'ImagesFileName' => 'File Name' 
         ), 
         'getCMSFields_forPopup' 
      );

Something like that at least :D

Avatar
SilverRay

Community Member, 167 Posts

17 August 2008 at 8:43pm

Thanks willr for your answer, I will try. I'm kind of swamped right now, but I'll report back, I promise!