Jump to:

5453 Posts in 1674 Topics by 1198 members

Customising the CMS

SilverStripe Forums » Customising the CMS » dynamically adding items to $db

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

Page: 1 2
Go to End
Author Topic: 1807 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    Re: dynamically adding items to $db Link to this post

    Ok so here it is, pretty simple in the end...

    EnviromentExtention.php

    <?php
    class EnviromentExtention extends DataObjectDecorator {
       
       function augmentSQL(SQLQuery &$query) {}
       
       function SanitizeString($String) {
          
          $tmp = preg_replace('/\s+/', '_', $String); // compress internal whitespace and replace with _
          $reserved = preg_quote('\/:*?!&"<>|', '/');//characters that are illegal
           //replaces all characters up through space and all past ~ along with the above reserved characters
          return preg_replace("/([\\x00-\\x20\\x7f-\\xff{$reserved}])/e", "", $tmp);

       }
       
       public function extraDBFields(){
          
          $db = array();
          if($Enviroments = DataObject::get("EnviromentPage")){
          
             foreach($Enviroments as $Enviroment)
             {
                $key = $this->SanitizeString($Enviroment->Title);
                $db[$key."_CategoryLink"] = "Varchar(255)";
             }      
          }
          return array('db' => $db);
       }   
       
       public function getCMSFields(){   
           $this->extend('updateCMSFields', $fields);
            return $fields;
       }    

       public function updateCMSFields(FieldSet &$f) {
          
          if($Enviroments = DataObject::get("EnviromentPage")){
             
             foreach($Enviroments as $Enviroment)
             {
                $key = $this->SanitizeString($Enviroment->Title);
                $f->addFieldToTab("Root.Content.Environments", new TextField($key."_CategoryLink"));
             }      
          }
       }

    }
    ?>


    Then in my _config.php I have this line:

    Object::add_extension('CategorytPage', 'EnviromentExtention');

    So now each time I add a new EnviromentPage and flush the DB, the Catagory page gets a new text field under the Enviroments tab called something like ebay_Catagorylink.

    Man I love SS

  • UncleCheese
    Avatar
    4085 Posts

    Re: dynamically adding items to $db Link to this post

    Nice job. The only thing I'd change is you have your SanitizeString method as a dynamic function. There's nothing dynamic about it -- its a simple input/output "factory" function as they're sometimes called. So cast it as static, and call it with self::SanitizeString()

    A lot of times for big projects I'll create a whole static class called Strings with all sorts of functions like that.

    Just a good practice, that's all.

    1807 Views
Page: 1 2
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.