Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Accessing database from page

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

Page: 1
Go to End
Author Topic: 820 Views
  • Phill
    Avatar
    Community Member
    75 Posts

    Accessing database from page Link to this post

    Hey

    Iv got a module or managing activities with the following fields:

       static $db = array(
          'Name' => 'Text',
          'Price' => 'Int',
          'CategoryID' => 'Text',
          'Type' => 'Text',
          'Description' => 'Text',
          'Parties' => 'Text',
           'ImageURL'=>'Text'   
       );

    everythings working fine on the admin side activities can be added, edited and deleted.

    But what i want to do is loop through all the activities with a categoryId of 1, somthing like

    <%control Activity %>
    <p>$Name</p>
    <% end_control %>

    any ideas on how i would achieve this as i cant seem to use DataObject inside my page.php

    Thanks

  • Phill
    Avatar
    Community Member
    75 Posts

    Re: Accessing database from page Link to this post

    hi iv got a steg closer with

    function phill_test()
    {
       $SQL_id = (int) 1;
       $page = DataObject::get('SiteTree');
       return $page;
    }

    this works displaying a list of all the pages but if i change SiteTree to Activities it doesnt anything (rest of page still dislpays fine) but there are entries in the db, any ideas on what im doing wrong?, is there possible anything i need to add to the activities class?

    thanks again

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Accessing database from page Link to this post

    Is CategoryID supposed to be text?

    Your function should look something like this:

    function getActivitiesWithCategory($category = "") {
       $category_SQL = Convert::raw2sql($category);
       return DataObject::get("Activities", "CategoryID = '{$category_SQL}'");
    }

    In your template, you can then call:

    <% control ActivitiesWithCategory(SomeCategoryName) %>
       This activity is called $Name
    <% end_control %>

  • Phill
    Avatar
    Community Member
    75 Posts

    Re: Accessing database from page Link to this post

    Thanks iv been able to solve the problem with your code

    820 Views
Page: 1
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.