17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1240 Views |
-
How to show image name or url in ManyManyComplexTableField?

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!
-
Re: How to show image name or url in ManyManyComplexTableField?

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
-
Re: How to show image name or url in ManyManyComplexTableField?

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!
| 1240 Views | ||
|
Page:
1
|
Go to Top |


