17488 Posts in 4473 Topics by 1978 members
| Go to End | Next > | |
| Author | Topic: | 4860 Views |
-
Sorting Articles

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. -
Re: Sorting Articles

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.
-
Re: Sorting Articles

18 January 2008 at 12:10am Last edited: 18 January 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. -
Re: Sorting Articles

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
-
Re: Sorting Articles

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?
-
Re: Sorting Articles

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. -
Re: Sorting Articles

18 January 2008 at 11:42am Last edited: 18 January 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"); -
Re: Sorting Articles

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.
| 4860 Views | ||
| Go to Top | Next > |



