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.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Detail an DataObject

Go to End

2 Posts   579 Views


Community Member, 8 Posts

20 October 2012 at 11:18pm

Hi!I'm a newcomer,I have a problem.I need your help.
I have class Acrticle
class Article extends DataObject
static $db=array('Title'=>'Varchar(100)',

static $has_one=array('Categories'=>'Categories');
class Categories:
class Categoriesextends Page
static $has_many=array('Articles'=>'Article');

public function getCMSFields()



$baivietfield=new GridField('Articles','Article',$this->Articles(),$config);


return $fields;
class Loai_Controller extends Page_Controller
After I show all article of each category in template,i want to read detail that article.How can do that?


Community Member, 20 Posts

23 October 2012 at 7:42pm

Edited: 23/10/2012 7:45pm

I'm not 100% sure if this is what you mean, but I created a similar listing. It loops through the categories and shows all of its items and some of its details belong and so on.

In my case the categories are data objects as well. The relation between articles and categories are the same as in your case. The articles are held by a category holder page (has many categories).

It's quiet simple to list the items, it can be done in the template - this should be your category holder page.

<!-- loops through categories from has many relation -->
<% loop Categories %>
<!-- loops through articles from has many relation - scope within the loop is on the article -->
<% loop Articles($ID) %>
<a href="$Link">$Title</a>
<% end_loop %>
<% end_loop %>

I don't know if really needed, the category dataobject contains the following method to return its articles:

public function Articles($category_id) {
return DataList::create('Article')->filter('CategoryID', $category_id)->sort('Title');