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 /

Jquery Custom search in Silverstripe


Reply


4 Posts   2306 Views

Avatar
acefx

Community Member, 8 Posts

30 December 2009 at 8:06pm

Hello everyone,

Hi everyone, I created a search that worked normally on my system using JQuery (Ajax) and CodeIgniter Framework.
The problem i have been having is how to do thesame in Silverstripe cause i dont know how where my post request will go (the controller) and also which table in the database.

I simple used jquery's $.post('url', 'request', 'callaback') and how it will work in Silverstripe i cant figure out yet.

Please is there a module for it or anything that can help, cause i hate the idea of constant realoading of page each time there is a search request.

Thanks

Avatar
markguinn

Community Member, 13 Posts

3 January 2010 at 8:31am

The default search form submits to something like "/home/SearchForm?Search=search+terms+go+here". The "/home/" bit can actually be almost any page, because "SearchForm" is an action method on the ContentController class.

If you want to do something similar to what you did in CodeIgniter, you can create a new controller class (descended from Controller, placed in mysite/code - or anywhere really). Then add something like this to your mysite/_config.php:

Director::addRules(100, array(
'team/$Action/$ID' => "Team_Controller",
));

So then /team/search would map to the search() function in Team_Controller. Hope that helps.

Mark

Avatar
acefx

Community Member, 8 Posts

12 January 2010 at 1:49am

Thanks man it helped,

but the major issue i have are:

where am i searching in the Database (I mean table name), cause the whole database seem confusing, though i am searching site_tree for now,

Also if i want to create a model, where do i create it cause its not advisable to write ur SQL query in the controller file

Thanks

Avatar
markguinn

Community Member, 13 Posts

13 January 2010 at 3:41am

The normal way to do models/controllers is to put them both in the same file in the mysite/code directory. (That's not strictly necessary though. You could create mysite/models and mysite/controllers like you do in other frameworks too if you want models and controllers separated.) If you're creating a "team" model, you'd create "Team.php" and it would contain two classes:

class Team extends DataObject {
static $db = array(
// fields defined here - see docs
);
}

class Team_Controller extends Controller {
function do_something() {
return $this->renderWith('Team','Page'); // this is going to look for Layouts/Team.ss for 'inner' content and Page.ss as the outer stuff
}
}

If you want the item to be part of the CMS structure, the model should extend Page (which extends Sitetree, which extends DataObject), and the controller should extend Page_Controller.

As for database tables, a dataobject can actually be split across several tables because of inheritance. Sitetree_Live is the main table for what is actually being displayed on the site. The easiest way to do it is through DataObject::get, which handles all the joins for you. You can do:

$SQL_search = Convert::raw2sql($searchstring);
$teams = DataObject::get('Team', "Title like '%$SQL_search%' or Content like '%SQL_search%'");

Check out the docs for sort and limit options on DataObject::get. Hope that answers your question more thoroughly. It's a little different paradigm than a lot of MVC frameworks (I was working with Kohana before Silverstripe), but you get used to it pretty quickly.