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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Nested controls from model admin


Go to End
Reply


4 Posts   1190 Views

Avatar
mschiefmaker

Community Member, 187 Posts

9 September 2009 at 8:06pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 September 2009 at 2:24am

Edited: 10/09/2009 2:25am

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 %>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 September 2009 at 2:26am

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

<% control getFaq %>

becomes

<% control Faq %>

Avatar
mschiefmaker

Community Member, 187 Posts

12 September 2009 at 10:31am

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

Cheers

MM