Jump to:

23358 Posts in 17873 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Using DataObjects without DB Table

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

Page: 1
Go to End
Author Topic: 1930 Views
  • banal
    Avatar
    Community Member
    901 Posts

    [SOLVED] Using DataObjects without DB Table Link to this post

    Hi all

    In a project I'm creating temporary DataObjects, which are being built dynamically using the SQLQuery Class:
    http://doc.silverstripe.org/doku.php?id=sqlquery#transforming_a_result_to_dataobjectset

    Since these DataObjects don't have to be persistent, I was wondering if there's a way to prevent the creation of a DB Table for my DataObject Class?

    Any pointers are greatly appreciated.

  • jam13
    Avatar
    121 Posts

    Re: [SOLVED] Using DataObjects without DB Table Link to this post

    I had a similar problem a few weeks ago where I wanted to create dataobjects which were session backed rather than db backed.

    After playing around with ViewableData (the parent class) for a few days I came to the conclusion that the gulf between dataobjects and viewabledata was so large that it was easier to just use the db as a session store and clean things up later with a script.

    Jamie

  • banal
    Avatar
    Community Member
    901 Posts

    Re: [SOLVED] Using DataObjects without DB Table Link to this post

    Hey Jamie

    Thanks for your reply. The DB Table isn't to much of an issue, since it just sits there and does nothing in my case ;) I don't even have to clean things up, but the table is still redundant.
    I was wondering why there isn't a table for "DataObject" itself... I think it might be possible to specify if a table should be created or not, I just wasn't able to figure out when and where this happens.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: [SOLVED] Using DataObjects without DB Table Link to this post

    Hello

    I figured it out. I had to override the requireTable method of the DataObject... no DB-Table is being created if I use the following code:

    public function requireTable(){
       DB::dontRequireTable($this->class);
    }

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: [SOLVED] Using DataObjects without DB Table Link to this post

    This is very interesting. Can I ask you guys what your guys were up to with unstored and/or transient objects?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: [SOLVED] Using DataObjects without DB Table Link to this post

    Hi dalesaurus

    Sure. It's a rather simple thing that I did and could also have been solved otherwise (as always).
    I have a page, that holds Events (DataObjects). This page only displays events from the current year. Events from previous years aren't displayed. To still be able to view older events, there should be an archive for every year. The year has to be selectable from the navigation (second level).

    So that's what I did:
    EventPage -> Holds all Event DataObjects
    ArchivePage -> Pulls DataObjects from the EventPage

    So far so good, no transient DataObjects till now.
    I used them to create a dynamic Sub-Navigation with the available archived Years under ArchivePage. Therefore I overrode the Children method in the ArchivePage to return a dynamically created DataObjectSet with the available years. To achieve that, I made use of the SQLQuery class and the handy buildDataObjectSet method. See: http://doc.silverstripe.org/doku.php?id=sqlquery#transforming_a_result_to_dataobjectset

    To make the entries in the set behave like a regular child page, I created a DataObject with all required methods like: Link, Title, MenuTitle, LinkingMode etc.
    Since these DataObjects are created on request (depending on years where events took place), they don't need to be stored in the DB.

    Not sure if my explanation was clear and/or of any use to you ;)

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