Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Searching few tables in one shot

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

Page: 1
Go to End
Author Topic: 582 Views
  • Victor
    Avatar
    Community Member
    128 Posts

    Searching few tables in one shot Link to this post

    If we have few tables with the same fields and want to present results together we can do something like this

    function PeopleSearchForm()
    {
    return new Form(
    $this,
    "PeopleSearchForm",
    new FieldSet(new TextField($name='FirstName',$title='First Name starts from', $value='',$maxLength='15'), new TextField('LastName','Last Name starts from')),
    new FieldSet(new FormAction('doPeopleSearch','People Search'))
    );
    }

    function doPeopleSearch($data, $form)
    {
    $results = new DataObjectSet();
    $resultsf = DataObject::get("FacultyPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%'");
    $resultss = DataObject::get("StaffPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%');
    $resultst =(DataObject::get("StudentPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%'"));
    $results->merge($resultsf);
    $results->merge($resultss);
    $results->merge($resultst);
    $results->sort('lastname');
    return $this->customise(array('PeopleSearchResults' => $results))->renderWith(array('PeopleSearch_results','Page'));
    }

    presenting results through

    <div class='typography'>
    <% if PeopleSearchResults %>
    <ul>
    <% control PeopleSearchResults %>
    <li><a href="$Link">$firstname $lastname</a></li>
    <% end_control %>
    </ul>
    <% else %>
    <font color="#FF0000">There were no results that matched your query.</font><p>
    <% end_if %>
    </div>
    <div>
    $PeopleSearchForm

    </div>

    However it would not work if tables contain different fields and we want to present Faculty, Students, Staff separately.

    Trying

    function doPeopleSearch($data, $form)
    {
    $resultsf = DataObject::get("FacultyPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%'","`lastname`");
    return $this->customise(array('FacultySearchResults' => $resultsf))->renderWith(array('PeopleSearch_results','Page'));

    $resultss = DataObject::get("StaffPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%'","`lastname`");
    return $this->customise(array('StaffSearchResults' => $resultss))->renderWith(array('PeopleSearch_results','Page'));

    $resultst = DataObject::get("StudentPage"," `lastname` LIKE '".$data['LastName']."%' AND `firstname` LIKE '".$data['FirstName']."%'","`lastname`");
    return $this->customise(array('StudentSearchResults' => $resultst))->renderWith(array('PeopleSearch_results','Page'));
    }

    and trying to output with

    <h2>Faculty</h2>
    <% if FacultySearchResults %>
    <ul>
    <% control FacultySearchResults %>
    <li><a href="$Link">$firstname $lastname</a></li>
    <% end_control %>
    </ul>
    <% else %>
    <font color="#FF0000">There were no results that matched your query.</font><p>
    <% end_if %>

    <h2>Staff</h2>
    <% if StaffSearchResults %>
    </div>

    .....
    ....

    we see that only FacultySearchResults are returned non-empty.

    How to fix it?

    Thank you in advance. Victor

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