Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Detail an DataObject

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

Page: 1
Go to End
Author Topic: 387 Views
  • bubu333
    Community Member
    8 Posts

    Detail an DataObject Link to this post

    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?

  • johannes
    Community Member
    20 Posts

    Re: Detail an DataObject Link to this post

    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');

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.