Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » ComplexTableField for self?

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

Page: 1
Go to End
Author Topic: 918 Views
  • Bambii7
    Avatar
    Community Member
    254 Posts

    ComplexTableField for self? Link to this post

    Hi all, I've been looking at the options availible to the admin for data fields. They're all pretty neat!
    My mind is having a field day at the posiblitys the dataobject relationships are opening up.

    Unfornately for me and everyone else I'm still quite novice. And my head is still coming to terms with it all. Any advice on setting up a comlextablefield for the current page? I'd like to associate a complex table and all it's entrys to only one page.

    I've been reading and playing all day, and while having a lot of fun I am getting a little brain dead. With out really thinking I tried the following

    class Project extends Page {
       
       static $db = array(
          'FirstName' => 'Text',
          'Lastname' => 'Text',
          'Nationality' => 'Text'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $tablefield = new ComplexTableField(
             $this,
             'MyStudent',
             'Project',
             array(
                'FirstName' => 'First Name',
                'Lastname' => 'Family Name',
                'Nationality' => 'Nationality'
             ),
             'getCMSFields_forPopup'
          );
          $tablefield->setParentClass(false);
          $fields->addFieldToTab( 'Root.Content.Student', $tablefield );
          
          return $fields;
       }

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FirstName', 'First Name' ) );
          $fields->push( new TextField( 'Lastname' ) );
          $fields->push( new TextField( 'Nationality' ) );
          return $fields;
       }

    }

    Don't laugh, I actually thought it might work....... Then realised db array will only have one field for each page in the databased, then further realised using Project as the source class would cause issues when saving which it did

    I cant fathom how to get it working.... any pointers or links to the answers for all questions would be amazing.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: ComplexTableField for self? Link to this post

    OMG I'm loving it like Mac Donalds, well maybe not like Mac Donalds more like a really good home cooked meal or s*x.

    It only took me like 6 hours to figure out WOW, but I'm stoked with the result and learnt even MORE about silverstripe

          $myTableField = new TableField(
             'MyStudent',
             'Student',
             array(
                'FirstName'=>'First Name',
                'Lastname' => 'Family Name',
                'Nationality' => 'Nationality',
             ), // fieldList
             array(
                'FirstName'=>'TextField',
                'Lastname'=>'TextField',
                'Nationality'=>'TextField'
             ), // fieldTypes
             'MyProjectID', // filterField (legacy)
             $this->ID
          );
                
          $myTableField->setExtraData(array(
           'MyProjectID' => $this->ID ? $this->ID : '$RecordID'
          ));   

    Those arn't the fields I was after but all the functionality baby yeah.

    The TableField above was used with a mildly edited Student Class from the 5th tutorial http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management

    class Student extends DataObject {
       
       static $db = array(
          'FirstName' => 'Text',
          'Lastname' => 'Text',
          'Nationality' => 'Text'
       );
       
       static $has_one = array(
          'MyProject' => 'Project'
       );
       
       function MyProject() {
          return DataObject::get( 'Project', "`MyStudentID` = '{$this->ID}'" );
       }
       
       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FirstName', 'First Name' ) );
          $fields->push( new TextField( 'Lastname' ) );
          $fields->push( new TextField( 'Nationality' ) );
          return $fields;
       }
       
       function forTemplate() {
          $template = 'GSOCPerson';
          return $this->renderWith( $template );
       }
       
    }

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: ComplexTableField for self? Link to this post

    PS

    For anyone who may have followed that heres the function I used to render the tables, although I've changed the class names which will mess it up a little. If any one know of a cleaner way to do this please let me know.

       
       function tableData() {
          $submitLink = DataObject::get("Pricing", "ThispriceID = $this->ID");   
          $output = $this->makeTable($submitLink);
          return $output;
       }
       
       private function makeTable($PricingData) {
          
          $output .= '<table cellpadding="0" cellspacing="0" border="0">';
          
          foreach($PricingData as $pricing) {
             if ($pricing->Isheading)
             $output .= '<tr class="tableHeading">';
             else
             $output .= '<tr>';
             
             $output .= '<td>'.$pricing->Columnone.'</td>';
             $output .= '<td>'.$pricing->Columntwo.'</td>';
             $output .= '<td>'.$pricing->Columnthree.'</td>';
             $output .= '<td>'.$pricing->Columnfour.'</td>';
             $output .= '</tr>';
          }
          
          $output .= '</table>';
          
          return $output;
          
       }

    918 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.