I have two data objects parent
and child
with parent
having many child
ren. Child
has a few boolean
s that describe it's state and should only be displayed on this particular page if in a certain state.
In my template I have:
<% with $Parent %>
<% if $Child %><h1>Children</h1>
<% loop $Child %>
<p>$Child.Title</p>
<% end_loop %>
<% end_if %>
<% end_with %>
However this will obviously show all the Child
ren, I can put a filter on it:
<% $Child.Filter('Show', '1') %>
However I can't do multiple items for the filter (at least that I can figure out), so I thought I'd make a function on the DataObject
that would be the derived boolean to show it, however this isn't something I can filter on.
Whats the best way to achieve what I'm trying to do?
Say these are our Child
and Parent
classes:
Child
class Child extends DataObject {
private static $db = array(
'Title' => 'Varchar(1024)',
'Awesome' => 'Boolean',
'Fun' => 'Boolean'
);
private static $has_one = array(
'Parent' => 'Parent'
);
}
Parent
class Parent extends DataObject {
private static $db = array(
'Title' => 'Varchar(1024)'
);
private static $has_many = array(
'Children' => 'Child'
);
}
We can add functions to the Parent
class to return various filtered Children
lists:
class Parent extends DataObject {
private static $has_many = array(
'Children' => 'Child'
);
public function getAwesomeChildren() {
return $this->Children()->filter('Awesome', true);
}
public function getAwesomeFunChildren() {
return $this->Children()->filter(array(
'Awesome' => true,
'Fun' => true
));
}
}
In our template we can call:
<% if $AwesomeChildren %>
<h1>Children</h1>
<% loop $AwesomeChildren %>
<p>$Title</p>
<% end_loop %>
<% end_if %>