5098 Posts in 1518 Topics by 1115 members
| Go to End | Next > | |
| Author | Topic: | 1542 Views |
-
dynamically adding items to $db

13 January 2009 at 1:08pm
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
-
Re: dynamically adding items to $db

13 January 2009 at 11:59pm
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
-
Re: dynamically adding items to $db

14 January 2009 at 4:05am
Are you running 2.3? Try extraStatics().
-
Re: dynamically adding items to $db

14 January 2009 at 4:08am
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?
-
Re: dynamically adding items to $db

14 January 2009 at 8:50am Last edited: 14 January 2009 8:55am
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
-
Re: dynamically adding items to $db

14 January 2009 at 9:50am
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.
-
Re: dynamically adding items to $db

14 January 2009 at 10:30pm
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
| 1542 Views | ||
| Go to Top | Next > |

