Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Jquery Custom search in Silverstripe

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

Page: 1
Go to End
Author Topic: 2261 Views
  • acefx
    Avatar
    Community Member
    8 Posts

    Jquery Custom search in Silverstripe Link to this post

    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

  • markguinn
    Avatar
    Community Member
    13 Posts

    Re: Jquery Custom search in Silverstripe Link to this post

    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

  • acefx
    Avatar
    Community Member
    8 Posts

    Re: Jquery Custom search in Silverstripe Link to this post

    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

  • markguinn
    Avatar
    Community Member
    13 Posts

    Re: Jquery Custom search in Silverstripe Link to this post

    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.

    2261 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.