Jump to:

3430 Posts in 1057 Topics by 734 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: 372 Views
  • bubu333
    Avatar
    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)',
                       '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?

  • johannes
    Avatar
    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 %>
    <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');
    }

    372 Views
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.