Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » latest dataobject to appear on home page

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 2233 Views
  • webtonic
    Avatar
    Community Member
    26 Posts

    latest dataobject to appear on home page Link to this post

    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");

    }
    }
    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: latest dataobject to appear on home page Link to this post

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

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

  • webtonic
    Avatar
    Community Member
    26 Posts

    Re: latest dataobject to appear on home page Link to this post

    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

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: latest dataobject to appear on home page Link to this post

    Hi,

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

  • webtonic
    Avatar
    Community Member
    26 Posts

    Re: latest dataobject to appear on home page Link to this post

    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

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: latest dataobject to appear on home page Link to this post

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

  • webtonic
    Avatar
    Community Member
    26 Posts

    Re: latest dataobject to appear on home page Link to this post

    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'

  • Willr
    Avatar
    Forum Moderator
    5483 Posts

    Re: latest dataobject to appear on home page Link to this post

    $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);
    }

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