CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Search Profiles: Search custom field with LIKE operator instead of =
Pages: [1]

Author Topic: Search Profiles: Search custom field with LIKE operator instead of =  (Read 3198 times)

areaten

  • Guest
Search Profiles: Search custom field with LIKE operator instead of =
November 23, 2009, 05:06:30 pm
Hi
 I have a custom profile as a search, and want to search a custom text field, but when it is exposed in the search profile the operator is "=" and I want it to be "LIKE" so that it doesn't just provide exact matches. Much like the contact name field which has a LIKE operator. Is there a way to do this? Or somewhere in the code?
Thanks for any suggestions

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Search Profiles: Search custom field with LIKE operator instead of =
November 23, 2009, 07:13:09 pm

you'll need to modify the code here:

CRM/Core/BAO/CustomQuery.php

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

areaten

  • Guest
Re: Search Profiles: Search custom field with LIKE operator instead of =
November 24, 2009, 02:12:29 pm
Thanks lobo.

I don't know php so I'm at a loss with this. I've had a look and I'm guessing
case 'String':
on line 311?

Testing my search profile again, it seems that by adding the %term% wildcards turns the search operator into LIKE and gives me the desired results, so really I guess what I'm after is the custom field search to automatically have wildcards added to the value entered..

mylo1882

  • Guest
Re: Search Profiles: Search custom field with LIKE operator instead of =
December 14, 2009, 04:34:31 am
Could anyone point out what code needs changing to achieve the above? Custom data - Alphanumeric - Text. I would like for the search to insert % wherever there is a space and at the begining and end of the search.
e.g. 'please search this' would search for '%please%search%this%'
Code: [Select]
                case 'String':
                    $sql = "LOWER($fieldName)";
                    // if we are coming in from listings,
                    // for checkboxes the value is already in the right format and is NOT an array
                    if ( is_array( $value ) ) {
                        require_once 'CRM/Core/BAO/CustomOption.php';

                        //ignoring $op value for checkbox and multi select
                        $sqlValue = array( );
                        if ($field['html_type'] == 'CheckBox') {
                            foreach ( $value as $k => $v ) {
                                $sqlValue[] = "( $sql like '%" . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . $k . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . "%' ) ";
                            }
                            $this->_where[$grouping][] = implode( ' AND ', $sqlValue );
                            $this->_qill[$grouping][]  = "$field[label] $op $qillValue";
                        } else { // for multi select
                            foreach ( $value as $k => $v ) {
                                $sqlValue[] = "( $sql like '%" . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . $v . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . "%' ) ";
                            }
                            $this->_where[$grouping][] = implode( ' AND ', $sqlValue );
                            $this->_qill[$grouping][]  = "$field[label] $op $qillValue";
                        }                   
                    } else {
                        if ( $field['is_search_range'] && is_array( $value ) ) {
                            $this->searchRange( $field['id'],
                                                $field['label'],
                                                $field['data_type'],
                                                $fieldName,
                                                $value,
                                                $grouping );
                        } else {
                            $val = CRM_Utils_Type::escape( strtolower(trim($value)), 'String' );
                            if ( $wildcard ) {
                                $val = strtolower( addslashes( $val ) );
                                $val = "%$val%";
                                $op  = 'LIKE';
                            }
                            $this->_where[$grouping][] = "{$sql} {$op} '{$val}'";
                            $this->_qill[$grouping][]  = "$field[label] $op $qillValue";
                        }
                    }
                    continue;
Thank you

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Search Profiles: Search custom field with LIKE operator instead of =
December 14, 2009, 06:47:28 am

the code at the bottom of the snippet, i.e. the last else clause is where u'll need to make the changes

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

mylo1882

  • Guest
Re: Search Profiles: Search custom field with LIKE operator instead of =
December 15, 2009, 02:54:14 am
Thanks lobo,

Have been playing around with this. But I think it is much more complicated than my very limited PHP skills :). If anyone has any suggestions it would be most appreciated.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Search Profiles: Search custom field with LIKE operator instead of =
December 15, 2009, 08:34:41 am

you might want to consider hiring someone from http://civicrm.org/professional/

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

mylo1882

  • Guest
Re: Search Profiles: Search custom field with LIKE operator instead of =
December 16, 2009, 01:58:56 am
Ok will do. Our charity has a very limited budget so I was hoping this was maybe something simple for those who know PHP. :) I can see from looking into it its not that simple.

Thanks

Mylo

Chris7789

  • Guest
Re: Search Profiles: Search custom field with LIKE operator instead of =
March 01, 2010, 05:22:03 am
Got there in the end. Code below will make the necessary changes in 2 different ways.

Code: [Select]
$val = strtolower( addslashes( $val ) );
 
// works but would replace sequential spaces with multiple %
$val = str_replace( ' ', '%', $val );
 
// replaces sequential spaces with only one %
$val = preg_replace('/[ ]+/', '%', $val);
 
$val = "%$val%";

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Search Profiles: Search custom field with LIKE operator instead of =

This forum was archived on 2017-11-26.