Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

Controlling a Class on a specific 'Branch'


Go to End
Reply


11 Posts   1484 Views

Avatar
MagicUK

Community Member, 60 Posts

18 August 2011 at 6:57pm

Edited: 18/08/2011 6:57pm

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?

Avatar
martimiz

Forum Moderator, 1132 Posts

18 August 2011 at 11:17pm

Edited: 18/08/2011 11:17pm

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 :-)

Avatar
MagicUK

Community Member, 60 Posts

18 August 2011 at 11:50pm

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!

Avatar
martimiz

Forum Moderator, 1132 Posts

19 August 2011 at 2:53am

Edited: 19/08/2011 2:54am

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 %>

Avatar
MagicUK

Community Member, 60 Posts

19 August 2011 at 3:11am

Edited: 19/08/2011 3:24am

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!

Avatar
MagicUK

Community Member, 60 Posts

19 August 2011 at 5:40am

Edited: 19/08/2011 5:59am

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? :-(

Avatar
martimiz

Forum Moderator, 1132 Posts

19 August 2011 at 6:51am

Edited: 19/08/2011 6:55am

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?

Avatar
MagicUK

Community Member, 60 Posts

19 August 2011 at 6:54am

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.

Go to Top