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 /

Detail an DataObject


Reply


2 Posts   458 Views

Avatar
bubu333

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)',
                   'Author'=>'Varchar(100)',
                   'Content'=>'Text');
      
      static $has_one=array('Categories'=>'Categories');
   }
class Categories:
class Categoriesextends Page
   {
      static $has_many=array('Articles'=>'Article');
      
      public function getCMSFields()
      {
         $fields=parent::getCMSFields();
         
         $config=GridFieldConfig_RelationEditor::create();
         
         
         $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array('Title'=>'Title',
                                                             'Categories.Title'=>'Categories',
                                                             'Author'=>'Author'));
                                                            
         $baivietfield=new GridField('Articles','Article',$this->Articles(),$config);
         
         $fields->addFieldToTab('Root.Article',$baivietfield);
         
         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?

Avatar
johannes

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 %>
<h2>$Title</h2>
<ul>
<!-- loops through articles from has many relation - scope within the loop is on the article -->
<% loop Articles($ID) %>
<li>
<a href="$Link">$Title</a>
</li>
<% end_loop %>
</ul>
<% 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');
}