Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » I know what I want to do - I just don't know how

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1696 Views
  • KatB
    Avatar
    Community Member
    94 Posts

    I know what I want to do - I just don't know how Link to this post

    If there is a tutorial on this, please post the direction

    I want to create something similar to that in tutorial 2 with ArticlePage and ArticleHolder.

    I just want to be able to sort and display the children in a specific way using custom function/controls in ArticleHolder, for example, based on date and author.

    How do I write my own functions/controllers to do this? How do I gain access to ArticleHolder's children?

    I note in tutorial 3 that a Form object is created in the controller. When/How is that BrowserPollForm() called? Is that when a user loads the page?

    Also, please note a small inconsistency (yes I am pedantic) in tutorial 2.
    In the written text is ". The controller for a page should inherit from ContentController." And yet, ArticlePage_Controller inherits from Page_Controller, not ContentController.

    The class Page_Controller is not mentioned in all-classes (http://doc.silverstripe.com/doku.php?id=all-classes)

    Is this an accident, or deliberate? Does it mean something really profound and significant?

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    Allright, I think I have found a hint of what I want from various places

    So I create a new function like

    function NewsArticles(){
    if(!is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
    $doSet = DataObject::get(
    $callerClass = "ArticlePage",
    $filter = "",
    $sort = "Date AND Author",
    $join = "",
    $limit = "");

    return $doSet ? $doSet : false;
    }

    Q. what does this line do:
    if(!is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;

    http://doc.silverstripe.com/doku.php?id=temp-howto-pagination

    http://doc.silverstripe.com/assets/classes/sapphire/core/DataObject.html#get

    Then in my template I add in <%control NewsArticles %>

    How far off am I??

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    "Also, please note a small inconsistency (yes I am pedantic) in tutorial 2.
    In the written text is ". The controller for a page should inherit from ContentController." And yet, ArticlePage_Controller inherits from Page_Controller, not ContentController. "

    If you look at Page.php you will find the Page_Controller class extends ContentController. This is called 'inheritance' So ArticlePage_Controller extends Page_Controller (defined in Page.php) which then extends ContentController so as you can see it does inherit ContentController! just not completely direct, it has to go through the page controller first, which is a good thing as ArticlePage Controller will inherit methods from Content Controller but Also any methods on Page Controller. - http://au2.php.net/manual/en/keyword.extends.php

    I just want to be able to sort and display the children in a specific way using custom function/controls in ArticleHolder, for example, based on date and author.

    If you read the second tutorial you will come across DataObject.
    - http://doc.silverstripe.com/doku.php?id=datamodel . Check out that wiki page for some more info. But basically this code from tutorial 2 provides a good start for what you want

    function LatestNews() {
    $news = DataObject::get_one("ArticleHolder");
    return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", 5) : false;
    }

    Basically that piece of code gets your ArticleHolder, then it returns the ArticlePage children that match the articleholder id. Date DESC - is the SORT field, So it will return them sorted by the latest date. And the 5 is the LIMIT which we set at 5.

    All this info can be found on tutorial 2 and http://doc.silverstripe.com/doku.php?id=datamodel#querying_data

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    If you read the second tutorial you will come across DataObject.
    - http://doc.silverstripe.com/doku.php?id=datamodel . Check out that wiki page for some more info. But basically this code from tutorial 2 provides a good start for what you want

    But the code from tutorial 2 explains how to get access to ArticlePage_Holder's children from the home page, not from within ArticlePage_Holder.

    Surely you wouldn't need to gain access to ArticlePage_Holder IN ArticlePage_Holder. Sorting children, in various ways, numerically, alphabetically, ascending, descending, etc, must be quite a common thing to do.

  • Andy
    Avatar
    230 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    You should be able to get the children like so:

    $children = $this->Children();

    Hope this helps.

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    Where does the Children() method come from?

    Its not in DataObject, Controller, ContentController, SiteTree

    Is it a fair assumption that Children() returns an array?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: I know what I want to do - I just don't know how Link to this post

    It's a tricky one! It comes from the Heirarchy decorator: http://doc.silverstripe.com/assets/classes/sapphire/core/Hierarchy.html

    Children() returns a DataObjectSet, which can be iterated like an array. 99% of lists of items in SS are returned as DataObjectSet objects. They're designed to be loaded straight into templates.

    1696 Views
Page: 1
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.