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

Sorting Articles


Go to End
Reply


11 Posts   5218 Views

Avatar
saimo

Community Member, 67 Posts

17 January 2008 at 8:40am

I want to sort the ArticlePages within an ArticleHolder, but I cannot figure out how exactly.
I've largely followed the first and the second tutorial, and I have also read a lot of information in the documentation, but all I find is http://doc.silverstripe.com/doku.php?id=datamodel#using_dataobjectrequest but I don't understand how to use it.

Avatar
Willr

Forum Moderator, 5513 Posts

17 January 2008 at 5:33pm

How did you want to sort them. Normally say if you want the latest NewsArticles you would do something like

function LatestNews($limit = "10") {
   return DataObject::get("News", "", "Date DESC", "", $limit);
}

Notice the 3 parameter in the DataObject call? This is the sort by field. So in our example above it would sort by the Date Field and DESC.

Avatar
saimo

Community Member, 67 Posts

18 January 2008 at 12:10am

Edited: 18/01/2008 12:14am

That seems to work pretty well, thanks.
But then I got a second question:
How do I make the number of items to be displayed configureable from the CMS? Or, as I'm not shure about that kind of functionality, lets turn it into a more generic question:
How do I easily access DB fields in php code?

EDIT:
I placed the code in the ArticleHolder class, Is that where it should be placed? It seems to work from ArticleHolder_Controller too.

Avatar
Willr

Forum Moderator, 5513 Posts

18 January 2008 at 1:25am

It depends where you want to call that function. It should always be in the controller section but for example if you had that in ArticleHolder you would only be able to call that function on the ArticleHolder page type so for example not blog entries or other pages

Avatar
saimo

Community Member, 67 Posts

18 January 2008 at 1:46am

Ok, I'll move it to the controller then, what kinds of things should be put into classes inheriting from Page?

Avatar
saimo

Community Member, 67 Posts

18 January 2008 at 11:35am

I've found another problem:
If I've got two ArticleHolder pages on my site, LatestNews will show Articles from all the ArticleHolder's.
This is how I think it could work but it doesn't:
function LatestNews($limit = "10") {
$data = $this->Children();
//return DataObject::get("ArticlePage", "", "Created DESC", "", $limit);
return $data->sort("Created DESC");
}
This is based on what I've seen in the documentation, but how should it be done?
Since I'm just scratching the surface of silverstripe here, this doesn't seem very logical at all, not really intuitive, at least not to me.

Avatar
dio5

Community Member, 501 Posts

18 January 2008 at 11:42am

Edited: 18/01/2008 11:46am

You'd probably end up with doing something like this:

function LatestArticles($limit)
{
return DataObject::get("ArticlePage", "ParentID = {$this->ID}", "Created DESC", "", $limit);
}

of course that's only working when you call it on ArticleHolder.

If you do a sort, I think you have to split the field and the sort order:

like

$data = $this->Children();
$data->sort("Created", "DESC");

Avatar
saimo

Community Member, 67 Posts

19 January 2008 at 2:03am

That worked great, thanks!
It's not so easy know how to use the parameters, though I had guessed it had to do with the second parameter.

$data = $this->Children();
$data->sort("Created", "DESC");
didn't work however, and I don't know why.

Thanks also for indirectly aswering one of my previous questions, how to access fields from php code.

Go to Top