Troy Thompson

Web Developer

Archive for May, 2008

  1. Searching on Multiple Fields in CakePHP

    Published
    Last Updated
    May 04 2008
    Tagged
    Content

    Lets say you want to provide users with a way to search for authors in CakePHP.

    Your authors table looks something like ( id, firstname, lastname, bio ).

    Usually when you want to search on something like this you would do the following.

    SELECT * FROM authors WHERE lname LIKE '%Jon%' OR lname LIKE '%Jon%';

    Great, you found Jon, but what if you replaced the above query with "Jon Doe"?

    SELECT * FROM authors WHERE lname LIKE '%Jon Doe%' OR lname LIKE '%Jon Doe%';

    Crap, we can't find Jon anymore. Okay how about the following:

    SELECT * FROM authors WHERE concat(fname,' ',lname) like '%Jon Doe%';

    Aha! We found Jon Doe again.

    But what if we searched for "Doe Jon". Again, we are not able to find Jon Doe.

    So now what? Well MySQL lets you build a fulltext index on multiple fields.
    Execute the following query.

    ALTER TABLE `authors` ADD FULLTEXT `fullname` (`fname`,`lname`);

    This allows us to do things like

    SELECT * FROM authors WHERE match(fname,lname) against ('doe jon' IN BOOLEAN MODE);

    And still get Jon Doe. :-)

    Now you can fight this fulltext search stuff all you want, but you're just going to end up with a huge query that doesn't perform as well as a fulltext query.

    If you have a database that does not support this, then you should consider upgrading if you can.
    Here is some code that creates a query that does a search on each field without fulltext indexing.

    Using a fulltext query in CakePHP is a matter of using the following condition ( yes, this is very hacky looking ):

    $paginator_options['conditions'] = array("1" => "1 AND MATCH(Author.fname,Author.lname) AGAINST('{$conditions['Author.search']}*' IN BOOLEAN MODE )");
    $this->set('authors', $this->paginate($paginator_options));
  2. Howto Create a hotkey to commit in Subclipse

    Published
    Tagged
    Content

    I use Subclipse as a plugin for Eclipse / Aptana, and there is no hotkey to perform a commit. This means anytime you want to commit something you have to navigate the file tree and then the context menus to commit, I'd rather not leave the keyboard to do a commit.

    So after some digging around I figured out how to add a hotkey, and I wanted to share.

    Open Eclipse / Aptana

    Window > Preferences

    In the filter box, type "keys"

    Scroll to "Save All" and double click it.

    In the Command field set change the Category to "SVN"

    Change the Name to "&Commit..."

    Change the key sequence to the key combo you want to perform a commit. I use alt + shift + s, but that might be weird for some folks.

    Click Add

    If you were successful you should be able to use your key combo to perform a commit for now on.