Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Nested controls from model admin

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1036 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Nested controls from model admin Link to this post

    I am a bit confused and unsure if I have made an error in setting up a model admin for an faq. I think model admin is great and the faq seemed ideal for administering it but when I come to display if I wonder if I have the wrong end of the stick and shouldn't have used modeladmin

    In more detail I have an faqadmin which enables the administrator to create frequent questions and answers and then to categorise them.

    <? class FaqAdmin extends ModelAdmin {

    protected static $managed_models = array(
    'Faq',
    'FaqCategory'
    );

    static $url_segment = 'faq'; // will be linked as /admin/faq
    static $menu_title = 'FAQ Admin';

    }
    ?>

    This is all working well my problem comes when I want to display the faq grouped by category. I can pull back the data

    function getFaqCategory() {
        return DataObject::get("FaqCategory", "", "", "", "");
    }
    function getFaq() {
        $category = DataObject::get_one("FaqCategory");
        return ($category) ? DataObject::get("Faq", "$category.ID", "DisplayOrder ASC", "", "") : 'false';

    but how do I nest the getFaq inside the getFaqCategory? What I have so far is:
    <% control getFaqCategory %>
    $Title
    <% control getFaq %>
          <p class="orange">Q: $Question</p>
          <p>$Answer</p>
       <% end_control %>
    <% end_control %>

    Do I need to nest the function instead? If so, how?

    Many thanks

    MM

  • UncleCheese
    Avatar
    4085 Posts

    Re: Nested controls from model admin Link to this post

    Faq.php

    class Faq extends DataObject
    {
    static $has_one = array ('FaqCategory' => 'FaqCategory');

    static $db = array (.....);
    // etc...

    }

    FaqCategory.php

    class FaqCategory extends DataObject
    {
    static $has_many = array ('Faqs' => 'Faq');

    static $db = array (....);

    //etc..

    }

    MyFaqPage.php (e.g.)

    class MyFaqPage extends Page
    {
    function FaqCategories()
    {
    return DataObject::get("FaqCategory");
    }
    }

    MyFaqPage.ss

    <% control FaqCategories %>
    <h2>$Title</h2>
    <% if Faqs %>
    <dl>
    <% control Faqs %>
    <dt>$Question</dt>
    <dd>$Answer</dd>
    <% end_control %>
    </dl>
    <% else %>
    No faqs.
    <% end_if %>
    <% end_control %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Nested controls from model admin Link to this post

    Also, avoid using getXXX() methods on your template to keep a distinction between template methods and controller/model methods.

    <% control getFaq %>

    becomes

    <% control Faq %>

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Nested controls from model admin Link to this post

    Thanks Uncle Cheese. This worked well and I think I am starting to understand it all.

    Cheers

    MM

    1036 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.