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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Filtering has_many relations from Parent DataObject


Go to End


3 Posts   2966 Views

Avatar
CodeGuerrilla

Community Member, 105 Posts

27 June 2008 at 7:30pm

Edited: 27/06/2008 8:11pm

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!

Avatar
CodeGuerrilla

Community Member, 105 Posts

30 June 2008 at 2:59pm

(bump) Anyone give me some pointers please?

Avatar
Willr

Forum Moderator, 5523 Posts

30 June 2008 at 5:51pm

-> Here is where i need to get the same dataObjectSet as Catgory but need the related Media items filtered by DISTINCT(MediaPublished) = $_GET['y'] <--

I did kinda a similar thing for a recent product - uses a many_many join tho

// $currentYear = Director::urlParam("ID") -- (yoursite.com/speaker/2 => 2)

$speakers = DataObject::get("SpeakerPage", '`SpeakerPage_Year`.Year = ' . $currentYear.'"', '', 'LEFT JOIN `SpeakerPage_Years` ON `SpeakerPage`.ID = `SpeakerPage_Years`.SpeakerPageID LEFT JOIN `SpeakerPage_Year` ON `SpeakerPage_Years`.SpeakerPage_YearID = `SpeakerPage_Year`.ID');