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.

Customising the CMS /

Accessing database from page


Reply


4 Posts   900 Views

Avatar
Phill

Community Member, 77 Posts

27 March 2009 at 12:54am

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

Avatar
Phill

Community Member, 77 Posts

27 March 2009 at 3:32am

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

Avatar
Hamish

Community Member, 712 Posts

27 March 2009 at 8:30am

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 %>

Avatar
Phill

Community Member, 77 Posts

28 March 2009 at 12:07am

Thanks iv been able to solve the problem with your code