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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to set up TableField


Reply

2 Posts   1051 Views

Avatar
klikhier

6 April 2011 at 7:42pm Community Member, 150 Posts

I would like to add a TableField to a tab in the CMS to simple add a list of hyperlinks (title + link) to a page. Problem: in my current setup when I change/add a hyperlink on a page, the change is reflected on all other pages as well. What should I change to prevent this from happening?

//mysite/code/Hyperlink.php

<?php

class Hyperlink extends DataObject
{
   static $db = array (
'Title' => 'Text',
'Link' => 'Text'
   );

   static $has_one = array (
      'CaseExamplePage' => 'CaseExamplePage'
   );

   public function getCMSFields_forPopup()
   {
      return new FieldSet(
new TextField('Title'),
new TextField('Link')
      );
   }

}

?>

//mysite/code/CaseExamplePage.php

<?php
class CaseExamplePage extends Page {

   public static $has_many = array(
'Hyperlinks' => 'Hyperlink'
   );

   public function getCMSFields() {
      $fields = parent::getCMSFields();
      
      // Add Hyperlinks tab
      $tblf = new TableField(
         //$this,
         'Hyperlinks',
         'Hyperlink',
         array('Title' => 'Title', 'Link' => 'Link'),
         array('Title' => 'TextField', 'Link' => 'TextField'),
         null,
         "Hyperlink.CaseExamplePageID",
         $this->ID
      );
      $fields->addFieldToTab('Root.Content.Hyperlinks', $tblf);

(...)

Avatar
JonYYC

16 May 2012 at 7:48am Community Member, 10 Posts

Check the parameters of the TableField constructor. I think it should be:

$tblf = new TableField(
'Hyperlinks',
'Hyperlink',
array('Title' => 'Title', 'Link' => 'Link'),
array('Title' => 'TextField', 'Link' => 'TextField'),
"CaseExamplePageID",
$this->ID
);

Jonathan.