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.

Data Model Questions /

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

Different Types Of News Items


Go to End


19 Posts   9184 Views

Avatar
CodeGuerrilla

Community Member, 105 Posts

20 January 2010 at 7:13pm

Edited: 20/01/2010 7:13pm

Not really sure what you mean by Mega drop down menus it depends on how many levels deep your SiteTree is going to be usually we just do drop downs to two levels (sometimes three)

have a look at Making a navigation system

Basically output a nested unorderd list and then use Suckerfish (Superfish jQuery is good)

<ul>
<% control Menu(1) %>
    <li><a href="$Link" class="$FirstLast $LinkingMode" >$MenuTitle</a>
    <% if Children %>
          <ul>
         <% control Children %>
              <li><a href="$Link" class="$FirstLast $LinkingMode" >$MenuTitle</a></li>
         <% end_control %>
          </ul>
    <% end_if %>
</li>
<% end_control %>
</ul>

By the way this might be better in its own thread I would also try searching for dropdown menus in the forum as I bet it has been brought up before.

Avatar
DanStephenson

Community Member, 116 Posts

20 January 2010 at 7:14pm

Silver, this is really a different subject. I have built a few sites with a mega drop down controlled with SS. Are you looking for something like the one I did on BucarsRV.com?

I don't know any tutorials, I built it by reviewing this page of the documentation and playing around a little.

Avatar
CodeGuerrilla

Community Member, 105 Posts

20 January 2010 at 7:16pm

I missed the $join param in the DataObject::get

foreach($categories as $category) {
array_push($articles, DataObject::get('ArticlePage', $category, 'Date DESC', NULL, 4));
} 

Avatar
DanStephenson

Community Member, 116 Posts

20 January 2010 at 7:35pm

Edited: 20/01/2010 7:36pm

I saw the missing Join too, which I added in just as you were posting, but still get the same error.

[User Error] Couldn't run query: SELECT `SiteTree_Live`.*, `Page_Live`.*, `ArticlePage_Live`.*, `SiteTree_Live`.ID, if(`SiteTree_Live`.ClassName,`SiteTree_Live`.ClassName,'SiteTree') AS RecordClassName FROM `SiteTree_Live` LEFT JOIN `Page_Live` ON `Page_Live`.ID = `SiteTree_Live`.ID LEFT JOIN `ArticlePage_Live` ON `ArticlePage_Live`.ID = `SiteTree_Live`.ID WHERE (ArticleCategory) AND (`SiteTree_Live`.ClassName IN ('ArticlePage')) ORDER BY Date DESC LIMIT 4 Unknown column 'ArticleCategory' in 'where clause'

*sighs*

Looks like I have a long night ahead of me still. Don't you hate it when code and you can't figure out why?

Below is my full ArticleHolder.php page

class ArticleHolder extends TwoColumnPage {
	static $db = array();
	static $has_one = array();
	
	static $default_child = "ArticlePage";
	static $allowed_children = array('ArticlePage');
	static $icon = "themes/swann/images/treeicons/newsholder";
}
 
class ArticleHolder_Controller extends Page_Controller {
	public function Categories() {
		$categories = DataObject::get('ArticleCategory');
		return $categories;
	}
	
	public function category() {
		//$categoryid = $_POST['categoryID'];
		$categoryid = $this->URLParams['ID'];
		if(empty($categoryid)) {
			$category = NULL;
		}else{
			$category = sprintf('ArticleCategoryID = %d', $categoryid);
		}
		$categories = $this->Categories();
		foreach($categories as $category) {
			//array_push($articles, DataObject::get('ArticlePage', $category, 'Date DESC', 4));
			array_push($articles, DataObject::get('ArticlePage', $category, 'Date DESC', NULL, 4)); 
		}
		return new ArrayData($articles);
	}
}

Avatar
CodeGuerrilla

Community Member, 105 Posts

20 January 2010 at 7:50pm

Edited: 20/01/2010 8:01pm

Did you do a dev=> build that SQL looks strange should be ArticleCategoryID

One thing to check that ArticleCategoryID has been created in ArticlePage DB table this is the $has_one relationship to ArticleCategory

*Edit have to go home now can take a look tomorrow if you don't crack it good luck!

Avatar
DanStephenson

Community Member, 116 Posts

21 January 2010 at 11:42am

I did a dev/build and also checked the database, and the field is OK. I don't know what's up. If you can take another look, I'd appreciate it.

Avatar
CodeGuerrilla

Community Member, 105 Posts

21 January 2010 at 3:59pm

Okay had to strip it right back but got it basically working you will need to change it to suit your needs:

ArticlePage.php

<?php
class ArticlePage extends Page {
  
   static $db = array();
   
   static $has_one = array(
      'Category' => 'ArticleCategory'
   );

	function getCMSFields() {
		$fields = parent::getCMSFields();
	
		$categories = DataObject::get("ArticleCategory")->toDropdownMap('ID', 'CategoryName');
		$fields->addFieldToTab("Root.Content.Main", new DropdownField(
			'CategoryID',
			'Category',
			$categories
			), 'Content');
		
		return $fields;
	}
} 
?>

ArticleHolder.php

<?php
class ArticleHolder extends Page {
  
   static $db = array();
   static $has_one = array();
   
   static $default_child = "ArticlePage";
   static $allowed_children = array('ArticlePage');
 
}

class ArticleHolder_Controller extends Page_Controller {
  
   function init() 
   {
		parent::init();
   }
   
   public function Categories() {
      $categories = DataObject::get('ArticleCategory');
      return $categories;
   }
   
	public function category() {

		$categoryid = $this->URLParams['ID'];
		
		$do = DataObject::get('ArticlePage',  sprintf('CategoryID = %d', $categoryid));
		$data = array('Articles'=>$do);
	
		return $this->customise($data)->renderWith(array('ArticleHolder', 'Page'));

	}
}
?>

ArticleCategory.php

<?php
class ArticleCategory extends DataObject {
	
	public static $db = array(
		'CategoryName' => 'Varchar(50)'
	);
	
}
?>

ArticleHolder.ss

<div id="Content" class="typography"> 
	<ul>
	<% control Categories %>
	<li><a href="{$Top.URLSegment}/category/{$ID}">$CategoryName</a></li>
	<% end_control %>
	</ul>
	
	<% control Articles %>
		$Title<br />
	<% end_control %>
		
</div>

Notes:

Make sure you manually enter some categories in ArticleCategory DB table, make sure you add some ArticlePage(s) and link them to the Categories

I would delete all the related tables and then do a dev => build

Good Luck!

Avatar
DanStephenson

Community Member, 116 Posts

21 January 2010 at 8:29pm

Works great! Thanks so much CodeGuerrilla!!

I am going to sit down and take a look at the code in ArticleHolder.php well tonight. I am still trying to get my head around the SS/Sapphire framework code. I want to try and find a way to show the latest 5 articles from all sections if no category is selected.