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.

Archive

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

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

How to show image name or url in ManyManyComplexTableField?


Reply

3 Posts   1512 Views

Avatar
SilverRay

14 August 2008 at 7:19pm Community Member, 167 Posts

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

14 August 2008 at 10:48pm Forum Moderator, 5511 Posts

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

17 August 2008 at 8:43pm Community Member, 167 Posts

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