Jump to:

5450 Posts in 1672 Topics by 1197 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: 1799 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    dynamically adding items to $db Link to this post

    Hi guys

    I want to be able to add items to the $db of a page based on a set of other pages. So I have a CatagoryPage type and an EnviromentPage type and i want the CatagoryPage's $db to add a key value pair into the array for each EnviromentPage that exists, so that when i create a new EnviromentPage and flush the db, it will add the appropriate fields to the CatagoryPage.

    Does anyone know of a way to dynamicaly add keys and value pairs to the $db array in this way?

    cheers

    Aram

  • Aram
    Avatar
    Community Member
    598 Posts

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

    Ok I think I need to use the DataObjectDecorator for this, but I am having trouble getting it to do anything!

    I have a class in mysite/code/PageDecorator.php

    class PageDecorator extends DataObjectDecorator {

       function extraDBFields() {
          
          return array(
             'db' => array('Value' => 'Varchar(255)')

          );
       }
       
    }

    and in mysite/_config.php i have

    DataObject::add_extension('Page', 'PageDecorator');

    But when I do a DB flush nothing happens?

    Am I missing something really obvious here?

    cheers

    Aram

  • UncleCheese
    Avatar
    4085 Posts

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

    Are you running 2.3? Try extraStatics().

  • UncleCheese
    Avatar
    4085 Posts

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

    As for the dynamic $db question, I'm having a hard time understanding why you would need to do that? This is an Enum field?

    Couldn't you just associate a Category with an Environment using a TypeDropdown?

  • Aram
    Avatar
    Community Member
    598 Posts

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

    Hi UncleCheese

    Thanks for helping with this. I am running 2.23 as this is a site which I made a while back and am now trying to add functionality. Here is the context of what i am trying to do:

    I created a site for a friend who runs an ebay store (http://www.urban-knights-ebay.com). It allows him to manage his product pages/faqs/postage info etc. easily and means creating a new product is super easy.

    Now he wants to be able to create different enviroments for different aution sites, so that had can go to /product1/ebay or /product1/ebid to get to the enviroment for that product, giving him the correct banner and links etc. Essentially each page would have multiple 'modes' or skins for different stores. I have managed to set it up so that when he creates a new enviroment he can set all the general stuff like header image etc, but the problem I have is some of the fields on the product are enviroment specific. This means that each time he creates a new enviroment, new fields need to appear on each product to allow him to set those variables.

    So for example if he creates a new enviroment page for 'ebid' then I need to add a field to the ProductPage class called Ebid_sampleLink.

    I need a function attached to the product page that can cycle throughe the current Enviroment pages and add the fields as neccecary.

    Can I do this with a DataObject decorator?

    cheers

    Aram

  • UncleCheese
    Avatar
    4085 Posts

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

    I see..

    Sure.. I would just use extraStatics() and mod your db array(). It will take some finagling, but I think you'll get it. The nice thing is that extraStatics() will put you in a function, so you can do whatever you want there. Here's some peudo code:

    class Environment extends DataObject
    {
    function extraStatics()
    {
    $db = array();
    foreach(DataObject::get("Environment") as $env)
    {
    $key = self::sanitize($env->Title);
    $db[$key."_Link"] = "Text";
    $db[$key."_SomeOtherField"] = "Text"
    }
    return array('db' => $db);
    }

    private static function sanitize($str)
    {
    // do some stuff here to make sure your string is a clean database field.
    return $str;
    }

    public function getCMSFields()
    {
    $f = parent::getCMSFields();
    foreach($this->dbFields() /* I believe that's the function. $this->stat('db') will work, too */ as $name => $type)
    {
    $f->addFieldToTab("Root.Content.EnvironmentStuff", new TextField($name));
    }
    return $f;

    }
    }

    Does that make sense? It will take a little futzing around, but I think you see the idea. Interesting problem to solve, though. Let me know how you do.. or ... how you "get on" as you folks like to say.

  • Aram
    Avatar
    Community Member
    598 Posts

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

    Hey UncleCheese, thanks a lot for that, I 'got on' great ;) Managed to get a hacky version working last night by using the extraDBFields and DataObjectDecorator (i'm still on 2.23 remember), will tidy it up tonight and post my code

  • UncleCheese
    Avatar
    4085 Posts

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

    Yeah, curious to see it. Nice work.

    1799 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.