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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

latest dataobject to appear on home page


Go to End


9 Posts   3259 Views

Avatar
webtonic

Community Member, 26 Posts

29 May 2010 at 10:05am

Edited: 29/05/2010 10:06am

Hi Guys

I am missing something really basic here, just can not see it
I have created a showcaseholder ,using dataObjectManager and this works perfectly.(1st time using this)

I am now trying to pull the latest image onto the home page and keep getting the error message "Line 21 in /home/stagton/public_html/mysite/code/HomePage.php"

this is line 21 of HomePage.php

return ($latestportfolio) ? DataObject::get("Portfolio", "ParentID = {$portfolio->ID}", "", "", $num) : false;

If you can debug this for me it would be great

thanks
Craig

here is my code
ShowcaseHolder.php
<?php
/**
* Defines the
*/
class ShowcaseHolder extends Page {
static $has_many = array (
'Portfolios' => 'Portfolio'
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this, // Controller
'Portfolios', // Source name
'Portfolio', // Source class
'PortfolioPhoto', // File name on DataObject
array(
'BusinessName' => 'Business Name',
'DomainName' => 'Domain Name'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);
$manager->setAddTitle("Image");
$f->addFieldToTab("Root.Content.Portfolio",$manager);
return $f;
}

}

class ShowcaseHolder_Controller extends Page_Controller {
public function init() {
parent::init();

Requirements::themedCSS("lightbox");
Requirements::javascript("mysite/javascript/jquery.lightbox-0.5.js");
Requirements::javascript("mysite/javascript/custom.litebox.js");
}

}

?>
portfolio.php
<?php

class Portfolio extends DataObject{
static $db = array(
'BusinessName' => 'Text',
'DomainName' => 'Text'
);
static $has_one = array(
'PortfolioPhoto' => 'Image',
'ShowcaseHolder' => 'ShowcaseHolder',

);

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('BusinessName','Business Name'),
new TextField('DomainName','Domain Name')
);
}
}

?>

?php
/**
* Defines the ArticlePage page type
*/
class HomePage extends Page {
static $db = array(

);

static $allowed_children = array('SliderPage');

}

class HomePage_Controller extends Page_Controller {

function ShowPortfolio ($num=1){
$latestportfolio = DataObject::get("Portfolio");
return ($latestportfolio) ? DataObject::get("Portfolio", "ParentID = {$portfolio->ID}", "", "", $num) : false;

}
public function init(){
parent::init();
Requirements::themedCSS("slider");

Requirements::javascript("mysite/javascript/jquery.cycle.all.2.74.js");
Requirements::javascript("mysite/javascript/slider.js");

}
}
?>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

29 May 2010 at 10:20am

DataObject::get() will return a DataObjectSet. You just want a single record.

$latestportfolio = DataObject::get_one("Portfolio");

Avatar
webtonic

Community Member, 26 Posts

29 May 2010 at 11:11am

Hi

Thanks, made the change, ran dev build, flush=1, still getting the same error

using version 2.4

Craig

code from homepage.php

19 function ShowPortfolio ($num=1){
20 $latestportfolio = DataObject::get("Portfolio");
21 return ($latestportfolio) ? DataObject::get("Portfolio", "ParentID = {$portfolio->ID}", "", "", $num) : false;

Error message Line 21 in /home/stagton/public_html/mysite/code/HomePage.php

Avatar
joshy

Community Member, 57 Posts

29 May 2010 at 11:28am

Hi,

What's the exact error message? And you don't seem to have followed UncleCheese's advice of using a get_one.

Avatar
webtonic

Community Member, 26 Posts

29 May 2010 at 11:33am

Sorry I pasted the incorrect code, this is what I have in homepage.php

function ShowPortfolio ($num=1){
$latestportfolio = DataObject::get_one("Portfolio");
return ($latestportfolio) ? DataObject::get_one("Portfolio", "ParentID = $portfolio->ID", "", "") : false;

}
the error message is

[Notice] Undefined variable: portfolio
GET /?flush=1

Line 21 in /home/stagton/public_html/mysite/code/HomePage.php

And I am new to this so If you could spell out exactly what I could change I would appreciate this

thanks
Craig

Avatar
joshy

Community Member, 57 Posts

29 May 2010 at 11:52am

Change $portfolio->ID to $this->ID and see where that gets you...

Avatar
webtonic

Community Member, 26 Posts

29 May 2010 at 12:08pm

code changed to

function ShowPortfolio ($num=1){
$latestportfolio = DataObject::get_one("Portfolio");
return ($latestportfolio) ? DataObject::get_one("Portfolio", "ParentID = $this->ID", "", "") : false;

}

new error

[User Error] Couldn't run query: SELECT "Portfolio"."ClassName", "Portfolio"."Created", "Portfolio"."LastEdited", "Portfolio"."BusinessName", "Portfolio"."DomainName", "Portfolio"."SortOrder", "Portfolio"."PortfolioPhotoID", "Portfolio"."ShowcaseHolderID", "Portfolio"."ID", CASE WHEN "Portfolio"."ClassName" IS NOT NULL THEN "Portfolio"."ClassName" ELSE 'Portfolio' END AS "RecordClassName" FROM "Portfolio" WHERE (ParentID = 100) ORDER BY SortOrder ASC LIMIT 1 Unknown column 'ParentID' in 'where clause'

Avatar
Willr

Forum Moderator, 5523 Posts

29 May 2010 at 12:29pm

$latestportfolio = DataObject::get_one("Portfolio");
return ($latestportfolio) ? DataObject::get_one("Portfolio", "ParentID = $this->ID", "", "") : false;

The code makes pretty much no sense. Your getting a random portfolio to check if you should return a portfolio. I think what you want to do is get your showcaseholder first then get one portfolio from that...

function ShowPortfolio ($num=1){ 
$latestportfolio = DataObject::get_one("ShowcaseHolder");
return ($latestportfolio) ? DataObject::get("Portfolio", "ShowcaseHolderID = '$latestportfolio->ID'", "", "", $num) : false; 

Or if you only have 1 showcase holder then getting that first is pointless - if you only have 1 portfolio section you can reduce that function down to

function ShowPortfolio ($num=1){ 
return DataObject::get('Portfolio', '','','',$num);
}

Go to Top