Hello all I have been evaluating SilverStripe to use with our client sites and begun by porting over a simple site, so far so good have all the basics working for the navigation/sub navigation highlighting of current page/section, saerch etc...
I have been replicating a News/Media Section of the website and managed to get the articles to be listed in their respective categories.
Also manged to get a list of DISTINCT years as navigation this is where I have run into problems trying to filter the Media by year as I am getting it via Category and the $has_many functionality.
See below to get what I am trying to do:
Category.php
class Category extends DataObject {
static $db = array(
'CategoryName' => 'Text'
);
static $has_many = array(
'MediaPages' => 'Media'
);
...
Media.php
class Media extends Page {
static $db = array(
'MediaTitle' => 'Text',
'MediaPublished' => 'Date',
'MediaSummary' => 'Text',
'MediaArchive' => 'Boolean'
);
static $has_one = array(
'MediaCategory' => 'Category',
'MediaImage' => 'Image',
'MediaFile' => 'File'
);
...
MediaHolder.php
class MediaHolder extends Page {
static $db = array(
);
static $has_one = array(
);
static $default_child = 'Media';
static $allowed_children = array('Media');
}
class MediaHolder_Controller extends Page_Controller {
function Categories()
{
if(isset($_GET['y']) && is_numeric($_GET['y'])) {
--> Here is where i need to get the same dataObjectSet as Catgory but need the related Media items filtered by DISTINCT(MediaPublished) = $_GET['y'] <--
} else {
return DataObject::get("Category");
}
}
function Years()
{
// build a new sql query
$sqlQuery = new SQLQuery();
$sqlQuery->select = array('YEAR(MediaPublished) AS y');
$sqlQuery->from = array("Media");
$sqlQuery->where = array("archive = 0");
$sqlQuery->distinct = true;
$sqlQuery->orderby = "y DESC";
$rawSQL = $sqlQuery->sql();
$result = $sqlQuery->execute();
// create a new data object set
$myDataObjectSet = new DataObjectSet();
// push our result into the data object set
foreach($result as $row) {
$myDataObjectSet->push(new ArrayData($row));
}
return $myDataObjectSet;
}
}
?>
MediaHolder.ss (Layout)
<div id="cols">
<div id="left_col">
<div id="left_nav">
<% include MediaMenu %>
</div>
</div>
<div id="right_col">
<div id="content_header" <% if HeaderImage %> style="background: url($HeaderImage.Filename) no-repeat" <% end_if %>>
<h2>$Title</h2>
</div>
<div id="content">
<% if Categories %>
<% control Categories %>
<% if MediaPages %>
<h3>$CategoryName</h3>
<% control MediaPages %>
$MediaTitle<br />
<% end_control %>
<% end_if %>
<% end_control %>
<% end_if %>
</div>
</div>
</div>
MediaMenu.ss (Include)
<% if Years %>
<ul>
<% control Years %>
<li><a href="$Top.URLSegment?y=$y">$y</a></li>
<% end_control %>
</ul>
<% end_if %>
This has me stumped most likely I am just approaching this the wrong way?
Anyways keep up the great work SS team!