7911 Posts in 1354 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Nested controls from model admin
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 924 Views |
-
Nested controls from model admin

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
-
Re: Nested controls from model admin

10 September 2009 at 2:24am Last edited: 10 September 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 %> -
Re: Nested controls from model admin

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 %>
-
Re: Nested controls from model admin

12 September 2009 at 10:31am
Thanks Uncle Cheese. This worked well and I think I am starting to understand it all.
Cheers
MM
| 924 Views | ||
|
Page:
1
|
Go to Top |
