Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Finding the search fireEvent call

I'm looking to modify the search function to limit the search to only some categories.

After hours of looking around, I came to this piece of code that builds out the where clause: $this->fireEvent('Search');
Can someone point me to the right direction in where this get's built out?


  • R_JR_J Ex-Fanboy Munich Admin

    That event is fired by the search model. What makes it quite hard to work with it is that there are no useful EventArguments.

    The SearchModel holds an array _SearchSql which comprises different sqls. They are filled by e.g. the VanillaSearchModel to the SearchModel. This way other addons, like the articles addon, can add their custom search sql. And the result is, that also custom content can be searched if the addon author implemented that.

    But sadly, you do not have access to those sqls. With the event AfterBuildSearchQuery you could get access to the (not yet) finished text sql, but you most probably do not want to start changing that by adding some regex transformations here...

    Look at the code:

    $this->EventArguments['Search'] = $Search;
    // Perform the search by unioning all of the sql together.
    $Sql = $this->SQL
        ->from('_TBL_ s')
        ->orderBy('s.DateInserted', 'desc')
        ->limit($Limit, $Offset)
    $this->EventArguments['Sql'] = $Sql;

    At the event "Search" there is no sql to change and at "AfterBuildSearchQuery" it is only a string that you shouldn't want work with.
    But what you can do is to add your Category restrictions beforehand!

    You can access the sql already before it is used by hooking into the search event and adding Gdn::sql()->where(). That will do the trick!

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    What is the syntax to put in that Where clause so it would perform a search for a string within a column (I can live with the performance in my small forum)?

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    @hgtonight - I tried to use the following but it didn't work. I suspect I am using the wrong syntax. Any clues?

    $Sender->SQL->Where($Searchcolumn.' LIKE ', $searchvalue);

  • R_JR_J Ex-Fanboy Munich Admin

    Try Gdn::sql() - >where and searchcolumn must be in the results. Look at how the sql looks at afterBuiltQuery

  • hgtonighthgtonight ∞ · New Moderator
    edited March 2016

    @rbrahmson Use the Gdn_SQLDriver::like() method:

    $sender->SQL->like('field_name', 'match text', 'side');

    The side can be 'left', 'right', or the default 'both'. It appends the appropriate characters for you.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    Got it. Apparently I did it the right way and then blew it up. Thanks for reassuring I was on the right track!

Sign In or Register to comment.