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.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
- Page 11(current)
- 2
- Next 10 entries
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.
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.
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
Ok, I'll move it to the controller then, what kinds of things should be put into classes inheriting from Page?
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.
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");
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.
- Page 11(current)
- 2
- Next 10 entries