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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

DataObject giving error when access admin


Reply


7 Posts   968 Views

Avatar
bunheng

Community Member, 78 Posts

17 June 2011 at 4:00pm

Edited: 17/06/2011 9:03pm

Good Afternoon,

I am arriving your website from SSBITS, I have a problem while trying to get DataObject items from children page.

I have the following page structure:

1. ProvincePage
- DistrictA
- DistrictB
- DistrictC
- News

2. ProvincePage
- DistrictD
- DistrictE
- DistrictF
- News

I wanted to get NewsItem (DataObject) from News to display only within each of ProvincePage and District Page. I am trying the following code but it is not work.

function LatestNews() {
$nid = DataObject::get_one("NewsPage", "ParentID = $this->ID");
if ($nid == '0'){
$nid = DataObject::get_one("NewsPage", "ParentID = $this->ParentID");
}
return ($nid)? DataObject::get(
"NewsItem", "NewsPageID = $nid->ID",
"LastEdited DESC", "", "5"
) : false;

}
}

Hope to see your kindness advice.

Thanks
Bunheng

Avatar
UncleCheese

Forum Moderator, 4094 Posts

18 June 2011 at 8:23am

What does "it doesn't work" mean?

Avatar
bunheng

Community Member, 78 Posts

19 June 2011 at 1:28pm

Hi,

I put the above function in my page.php and try to use access admin it return the following error:

------------------------------------------------------------------------
[User Error] Couldn't run query: SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."ParentID", "Page_Live"."IntroPhotoID", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID" WHERE ("SiteTree_Live"."ClassName" IN ('NewsPage')) AND (ParentID = ) ORDER BY "Sort" LIMIT 1 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 ') ORDER BY "Sort" LIMIT 1' at line 1
-------------------------------------------------------------------------

But for the front-end, I can retrieve records as i wanted. In case i delete that function (function LatestNews() ) from page.php the I can access the admin.

Thanks for your valued comment and advices.

Bunheng

Avatar
UncleCheese

Forum Moderator, 4094 Posts

20 June 2011 at 4:17am

That's a pretty awful looking function. What does the rest of your Page.PHP look like?

Avatar
bunheng

Community Member, 78 Posts

20 June 2011 at 12:35pm

Hi,

Thanks for your comment below is my Page.php code, the function that cause error is within LatestNews().

-----------------------------------------------------------------------------------
<?php

class Page extends SiteTree {

static $icon = "themes/tonlesap/images/sitetree/page";
public static $db = array(
);
public static $has_one = array(
'IntroPhoto' => 'BetterImage'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Photo", new ImageField("IntroPhoto", "Featured Photo"));
return $fields;
}

function LatestFeatures() {
$pid = DataObject::get_one("Page", "ParentID= $this->ID");
return ($pid) ? DataObject::get("FeaturePage", "ParentID = $pid->ID", "", "", "") : false;
}

function LatestAnnouncements() {
return DataObject::get(
"Announcement", "",
"LastEdited DESC", "", ""
);
}

function LatestNews() {
$nid = DataObject::get_one("NewsPage", "ParentID = $this->ID");
if ($nid == '0') {
$nid = DataObject::get_one("NewsPage", "ParentID = $this->ParentID");
}
return ($nid)? DataObject::get(
"NewsItem", "NewsPageID = $nid->ID",
"LastEdited DESC", "", "5"
) : false;
}
}

public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = true) {
$page = $this;
$parts = array();
$i = 0;
while (
$page
&& (!$maxDepth || sizeof($parts) < $maxDepth)
&& (!$stopAtPageType || $page->ClassName != $stopAtPageType)
) {
if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
if ($page->URLSegment == 'home')
$hasHome = true;
if (($page->ID == $this->ID) || $unlinked) {
$parts[] = ("<li class=\"current\">" . Convert::raw2xml($page->MenuTitle) . "</li>" );
} else {
$parts[] = ("<li><a href=\"" . $page->Link() . "\">" . Convert::raw2xml($page->MenuTitle) . "</a>");
}
}
$page = $page->Parent;
}

return implode(self::$breadcrumbs_delimiter, array_reverse($parts));
}

}

class Page_Controller extends ContentController {

function mynews() {
$myalatest = DataObject::get('NewsItem');
return $myalatest->renderWith('newsfilexml');
}

public static $allowed_actions = array(
'newsfilexml'
);

public function init() {
parent::init();

//Set our theme's root folder
$themeFolder = $this->ThemeDir();

//Add all the files to combine into an array
$CSSFiles = array(
$themeFolder . '/css/layout.css',
$themeFolder . '/css/typography.css',
$themeFolder . '/css/form.css',
);

//Set the folder to inside our theme so that relative css image paths work
Requirements::set_combined_files_folder($themeFolder . 'combinedfiles');

//Combine!
Requirements::process_combined_files("main.css", $CSSFiles);
Requirements::javascript('themes/tonlesap/scripts/jquery.min.js');
Requirements::javascript('themes/tonlesap/scripts/corner.js');
}

}

Thanks for your time and comment.

Bunheng

Avatar
UncleCheese

Forum Moderator, 4094 Posts

21 June 2011 at 4:53am

Check the syntax on your LatestNews() function. You have one too many closing braces, which means your classes aren't parsing correctly.

I'm not even sure what that function is supposed to do, but it probably belongs in the controller, not the model. Why are you comparing the result of a query to "0"? A DataObject::get() will never return "0".. It will return false, which is similar to 0, and your double equals will work, but something tells me that's not your intention.

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
bunheng

Community Member, 78 Posts

21 June 2011 at 11:57am

Hi,

Thank you for your kindness advice and comment now, all working fine, I have change "0" to false. The problem is conflict on URLSegment = "Security" :). Thanks for your dataobjectmanager module and valued time.

Best Regrads,
Bunheng