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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Multiple DataObject Problem


Go to End


10 Posts   2172 Views

Avatar
ambient

Community Member, 130 Posts

12 July 2011 at 6:57am

I had this in the wrong forum so I'm moving it to a new home here in 'General Questions'. Please please please let there be an answer here :)

Hi All,

I am trying to show different news summaries on different pages. I am not very good with PHP but am trying to improve.

I've used the news article from tutorial 2 as my foundation.
For example if I have a sports news page that i want to show on some pages
and then a local news page that i want to show on some other pages etc. etc.

The plan is to have a drop down box in the cms to choose which news section to display on each page.

The problem is I can't even get a second summery to show on any page. As soon as I replicate the code, changing function and control names, the page gets a server error with no explanation.

Below are some snippets of my code.
Could anyone please take a look and explain what I'm doing wrong or suggest a better way of doing it. I've been battling this for days :(

Page.php

function NewsListGreen($num=5) {
    $green = DataObject::get_one("NewsHolder");
    return ($green) ? DataObject::get("NewsGreenPage", "ParentID = $green->ID", "Date DESC", "", $num) : false;
}

Page.ss

<% control NewsListGreen %>
       
    
    <div class="News_listing_box"><!--start News_listing_box-->

        <div class="News_listing_img"><!--start News_listing_img-->
        <a href="$PageLink.Link">$Img</a>
        </div><!--end News_listing_logo-->
        
        <div class="News_link $BorderColor"><!--start News_listing_link-->
        <a href="$PageLink.Link"><h2>Read More</h2></a>
        </div><!--endNews_listing_link-->
            
		
    
  	</div>
    <% end_control %>

HomePage.php

	function NewsListGold($num=7) {
    $gold = DataObject::get_one("NewsHolder");
    return ($gold) ? DataObject::get("NewsGoldPage", "ParentID = $gold->ID", "Date DESC", "", $num) : false;
} 

HomePage.ss

<% control NewsListGold %>

        <div class="News_listing_box"><!--start News_listing_box-->

        <div class="News_listing_img"><!--start News_listing_img-->
        <a href="$PageLink.Link">$Img</a>
        </div><!--end News_listing_logo-->
        
        <div class="News_link $BorderColor"><!--start News_listing_link-->
        <a href="$PageLink.Link"><h2>Read More</h2></a>
        </div><!--endNews_listing_link-->        
           
  <% end_control %>

Avatar
Willr

Forum Moderator, 5523 Posts

12 July 2011 at 6:24pm

The problem is I can't even get a second summery to show on any page. As soon as I replicate the code, changing function and control names, the page gets a server error with no explanation.

See the forum FAQ - Server error is a generic message. Devmode will reveal more information about your error..

http://doc.silverstripe.org/sapphire/en/topics/debugging#dev-mode

Avatar
ambient

Community Member, 130 Posts

13 July 2011 at 1:51am

Hi Will,

Thanks for that. Thats going to help out bigtime :)

I ran the pages in question and the following error message showed. It's a little confusing for me but am I right in thinking the issue lies somewhere within my site tree code?

[User Error] Couldn't run query: SELECT "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."ParentID", "Page"."PageColor", "LstGreenPage"."StartDate", "LstGreenPage"."Company", "LstGreenPage"."BorderColor", "LstGreenPage"."LogoID", "LstGreenPage"."PageLinkID", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree" LEFT JOIN "Page" ON "Page"."ID" = "SiteTree"."ID" LEFT JOIN "LstGreenPage" ON "LstGreenPage"."ID" = "SiteTree"."ID" WHERE ("SiteTree"."ClassName" IN ('LstGreenPage')) AND (ParentID = 79) ORDER BY Date DESC LIMIT 5 Unknown column 'Date' in 'order clause'

Avatar
ambient

Community Member, 130 Posts

13 July 2011 at 2:59am

SUCCESS!!! Thanks Will, it turns out among other things I missed calling a variable correctly. D'uh!

A related issue is if the Control can be used with a variable? I'm trying it like this but it doesn't work

<% control NewsList$PageColor %>

Is there a way to do this?

Avatar
Willr

Forum Moderator, 5523 Posts

13 July 2011 at 5:12pm

Nope, not currently in 2.4. You'd have to have a control statement like control NewsListWithColor and then in that function reference $this->PageColor

Avatar
ambient

Community Member, 130 Posts

13 July 2011 at 9:44pm

Hi Will,

I'm kinda new to this, would you mind explaining that a little more.

Are you saying this should be the control:

<% control NewsListWithColor %>

And then add $this->PageColor somewhere in the functions below? Sorry, I'm a little confused

function NewsListGreen($num=5) { 
$green = DataObject::get_one("NewsHolder"); 
return ($green) ? DataObject::get("NewsGreenPage", "ParentID = $green->ID", "Date DESC", "", $num) : false; 
}

function NewsListBlue($num=5) { 
$blue= DataObject::get_one("NewsHolder"); 
return ($blue) ? DataObject::get("NewsBluePage", "ParentID = $blue->ID", "Date DESC", "", $num) : false; 
}

Avatar
Willr

Forum Moderator, 5523 Posts

14 July 2011 at 3:50pm

Edited: 15/07/2011 9:00am


So you would want to write your function like this instead

function NewsList($num=5) {

switch($this->PageColor) {
case 'blue':
$color = "NewsBluePage";
break;
case 'teal':
$color = "NewsTealPage";
break;
default:
$color = "NewsPinkPage";
}

return DataObject::get($color, "", "Date DESC", "", $num);
}

Depending on how you've structured your pages you could even make that simpler (i.e if those pages are simply siblings)

Avatar
ambient

Community Member, 130 Posts

15 July 2011 at 3:23am

Super!! Thanks Will!

I just had to make seperate switch($this->PageColor) { for each page color and take out the default:
$color = "NewsGoldPage";
as it was overriding the actual page colors and it's working great.
Thanks again man, this was turning into a heartbreaker ;)

function NewsList($num=5) {

		switch($this->PageColor) { 
		case 'Gold': 
		$color = "LstGoldPage"; 
		}
		
		switch($this->PageColor) { 
		case 'Purple': 
		$color = "NewsPurplePage";
		}
		
		switch($this->PageColor) { 
		case 'Magenta': 
		$color = "NewsMagentaPage";  
		}
		
		switch($this->PageColor) { 
		case 'Blue': 
		$color = "NewsBluePage"; 
		}
		
		
		return DataObject::get($color, "", "StartDate DESC", "", $num); 
		}

Go to Top