Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Different Types Of News Items

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

Page: 1 2 3
Go to End
Author Topic: 5036 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Different Types Of News Items Link to this post

    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.

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Different Types Of News Items Link to this post

    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.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Different Types Of News Items Link to this post

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

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

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Different Types Of News Items Link to this post

    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);
       }
    }

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Different Types Of News Items Link to this post

    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!

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Different Types Of News Items Link to this post

    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.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Different Types Of News Items Link to this post

    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!

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Different Types Of News Items Link to this post

    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.

    5036 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.