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.

Archive /

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

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

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


Reply


7 Posts   1722 Views

Avatar
KatB

Community Member, 94 Posts

24 July 2007 at 5:14pm

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?

Avatar
KatB

Community Member, 94 Posts

24 July 2007 at 7:45pm

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??

Avatar
Willr

Forum Moderator, 5513 Posts

24 July 2007 at 7:46pm

"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

Avatar
KatB

Community Member, 94 Posts

25 July 2007 at 11:47am

Edited: 25/07/2007 11:48am

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.

Avatar
Andy

230 Posts

25 July 2007 at 3:22pm

You should be able to get the children like so:

$children = $this->Children();

Hope this helps.

Avatar
KatB

Community Member, 94 Posts

25 July 2007 at 6:08pm

Where does the Children() method come from?

Its not in [url=http://doc.silverstripe.com/assets/classes/sapphire/core/DataObject.html]DataObject[/url], [url=http://doc.silverstripe.com/assets/classes/default/Controller.html]Controller[/url], [url=http://doc.silverstripe.com/assets/classes/default/ContentController.html]ContentController[/url], [url=http://doc.silverstripe.com/assets/classes/sapphire/core/SiteTree.html]SiteTree[/url]

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

Avatar
Sam

Administrator, 685 Posts

25 July 2007 at 8:04pm

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.