I'm building an archive page and I'm having trouble pulling and filtering my results by year.
The page type has a dropdown to select which year should be displayed
private static $db = array(
"SubType" => "Enum(array('2013', '2014', '2015', '2016', '2017', '2018'))"
);
The template calls a function GetFilms
public function GetFilms()
{
$films = DataObject::get("Film");
return $films;
}
If I use the function above it works but displays all the films.
When I try to modify it to filter using the year its been passed,
public function GetFilms($SubType)
{
$films = DataObject::get("Film","'Year' = '$SubType'");
return $films;
}
The page no longer displays any of the films. Does anyone know what I'm doing wrong?
Here's the code for displaying the results,
<% loop GetFilms($SubType) %>
<div class="film" data-title="$Title">
<a href="$Top.Link?film=$ID">
<div class="inner">
<h3 class="filmTitle">$Title</h3>
<img class="filmImg" src="$FilmImage.URL" alt="$Title" />
</div>
</a>
</div>
<% end_loop %>
Thank you for your help!
What I'd suggest is doing some debugging. You can add some points into GetFilms()
e.g. var_dump($SubType);
to see what the template is passing in. You should check that your Film DataObjects can be accessed manually by using dummy values, e.g. Film::get()->filter(['Year' => 2013])
, then once you've eliminated the data source and the method's input as problem areas you can piece it back together.
I'd also suggest moving away from DataObject::get
arguments to a chainable approach using DataList methods, e.g.:
public function GetFilms($SubType)
{
return Film::get()->filter(['Year' => $SubType]);
}