Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Controlling a Class on a specific 'Branch'

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 2
Go to End
Author Topic: 1311 Views
  • MagicUK
    Avatar
    Community Member
    60 Posts

    Controlling a Class on a specific 'Branch' Link to this post

    Hi Guys. Ran into a problem and was hoping you guys could help.

    I have a site that has an Event home page and then children page for that event.

    I have a page on one of those children that is getting data from another page on this level using in one of my template files:

    <% control Page(speakers) %>
                
    ...                     
                
    <% end_control %>

    Unfortunately I realise this is only taking the URL segment of that specific page. Therefore if I create an new Event with the same 'children' structure it sill only copies the variables from the original Event and not the new one.

    I guess what I am asking is how do I control values from a specific class which is on the same branch of my site? Or is there another way to do this?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    I'm not completely sure what you mean... Every Events page has a couple of children, and amongst those children there is one special data-child that (some of) the other children take data from? Something like this?

    Event 1
       -> data-child
       -> child
       -> child
    Event 2
       -> data-child
       -> child
       -> child

    If I'm completely wrong, pleas elaborate

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    You got it mate. For each event I want the child to only access a dataobject on the data-child class (as you call it) for the event that that perticular child is on. I've looked around an I'm guessing that it is some kind of dataobject::get() but I am just learning silverstipe an have not idea how to sort this! Help would be really appreciated!

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    There are many alternatives, this is just one: first make the datachild page recognizable, by creating a special pagetype 'DataChildPage' (or even by setting some $IsDataChild boolean).

    Next in your Page_Controller (this will return one(1) DataChildPage that has the same parent as the current page):

    function getDataChild() {
       return DataObject::get_one('DataChildPage', 'ParentID = $this->ParentID');
    }

    Next in your template something like:

    <% control DataChild %>
       Description <= or whatever
    <% end_control %>

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    Amazing! Thanks! So once i bring in the <% control DataChild %> ... <% end_control%> I will have access to the dataobjects that are within that page?

    Once last thing. I have the following code on another page that I stole from a tread on here which works great. (this is not in the page controller incidentally):

    function ShowForm(){
    $get = DataObject::get_one('SiteTree', "URLSegment = 'order-form'");
    return new UserDefinedForm_Controller($get);
    }

    I can change this like so to achive the same effect as your code above? Would I have to place this in the controller?:

    function ShowForm(){
    $get = DataObject::get_one('orderFormPage', 'ParentID = $this->ParentID');
    return new UserDefinedForm_Controller($get);
    }

    Thanks martimiz. I've been stressing about this all day and you have made me face life again. haha!

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    Hi Ive tried the code martimiz.

    This:

       function getDataChild() {
          return DataObject::get_one('SpeakersPage', 'ParentID = $this->ParentID');
       }

    And when visiting the page I'm getting a synax error:

    [User Error] Couldn't run query:...You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '>'ParentID')

    Can you help?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    Oops, sorry - a single-quote error

    function getDataChild() {
    return DataObject::get_one('SpeakersPage', "ParentID = '$this->ParentID'");
    }

    As for your other question, I would think so once the 'quote issue' is fixed... [EDIT] but if you're not on the current Page controller, it won't work. You'd need to obtain the current page's ParentID one way or the other. Where's that function located?

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: Controlling a Class on a specific 'Branch' Link to this post

    OK so multiple kudos to the IRC lads (scpi)

    I got a working function:

       function getDataChild() {
             return DataObject::get_one('SpeakersPage', "\"ParentID\" = " . $this->getField("ParentID"));
       }

    Many thanks.

    Now I'm trying to extend this type of function slightly:

    I used to have this: (which worked perfectly)

    function ShowForm(){
       $get = DataObject::get_one('SiteTree', "URLSegment = 'brochureform'");
       return new UserDefinedForm_Controller($get);
       }

    I don't want to strict-type it to a URL segment so I'm trying to say get page WHERE page has the same 'parent id' but AND $Title = 'Brochure Form'

       function ShowForm(){
       
        $get = DataObject::get_one('UserDefinedForm', "\"ParentID\" = " . $this->getField("ParentID"). " AND \"Title\" = " . $Title->'BrochureForm'));
    return new UserDefinedForm_Controller($get);
    }   

    But sadly it doesn't work. Any ideas why? I get no error message just a blank page. Even in dev mode.

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