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) »
  • Custom Queries no longer working (RESOLVED)
Pages: [1]

Author Topic: Custom Queries no longer working (RESOLVED)  (Read 4581 times)

Erich

  • I post occasionally
  • **
  • Posts: 84
  • Karma: 1
Custom Queries no longer working (RESOLVED)
June 04, 2013, 10:02:08 pm
I have 14 custom queries that were developed under an earlier version of CiviCRM that stopped working when we upgraded to 4.3.1.

I was able to get 13 of them to work by diddling with the where clause, but I am having trouble with the last one, whose where clause is simply:

((custom_group.custom_field LIKE '%val1%') OR
 (custom_group.custom_field LIKE '%val2%') OR
 (custom_group.custom_field LIKE '%val3%') OR
 (custom_group.custom_field LIKE '%val4%') OR
 (custom_group.custom_field LIKE '%val5%'))

where custom_group and custom_field should be self-explanatory, and valN are different two-digit field values. In the actual custom query script, verything is on one line; I just broke it up here for readability.

If it makes a difference for understanding what I used to be able to do, the custom field is a random-length string of the form:

delimiter [AA delimeter]*

where AA is a two-digit number (which happens to represent the last two digits of a year).

So, for one row, the custom field might look like:

delimiter 04 delimiter 08 delimiter

and for another row it might look like:

delimiter 04 delimiter 06 delimiter 08 delimiter 10 delimiter

and for a third row it might look like":

delimiter 04 delimiter 05 delimiter 06 delimiter 07 delimiter 08 delimiter

etc.

I am interested in being able to find all records where there is an odd-numbered year anywhere in the custom field... so:

((custom_group.custom_field LIKE '%05%') OR
 (custom_group.custom_field LIKE '%07%') OR
 (custom_group.custom_field LIKE '%09%') OR
 (custom_group.custom_field LIKE '%11%') OR
 (custom_group.custom_field LIKE '%13%'))

Any ideas why I would be getting "no matches found" (when I know there should be), or suggestions like "check your spelling" (the only difference between this query and any of the other 13 is the class name and the where clause) or "try a different spelling or use fewer letters" (I've tried a class name with fewer letters and it still doesn't work) or "make sure you have enough privileges in the access control system" (all the custom scripts have the same 775 permissions)?
« Last Edit: June 06, 2013, 06:48:03 am by Erich »

JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Custom Queries no longer working
June 05, 2013, 07:23:32 am
Hi Erich,

When you say "custom queries", how are you using these queries?  Are they built into custom reports?  Or are they run directly against MySQL?

If it's against MySQL, then it's not really a Civi question - and if it's a custom report or similar, I'd say the first thing to do is to run a MySQL query directly against the database to isolate if it's Civi-related or MySQL-related.

If it IS MySQL related, then it'd probably help to to a DESCRIBE `custom_group` command and post that, along with the actual SQL that's failing.

Jon
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

Erich

  • I post occasionally
  • **
  • Posts: 84
  • Karma: 1
Re: Custom Queries no longer working
June 05, 2013, 08:31:50 pm
Built into custom reports (based on the CiviCRM v3.4 version of PostalMailing.php from about a year or so ago... see Reply #13 on http://forum.civicrm.org/index.php/topic,24014.msg105441.html for specifics)

Applicable details:

From all the working queries and the one non-working query (i.e. I always show the same columns):

        $this->_columns = array( ts('id')             => 'contact_id'  ,
                                 ts('Name')         => 'sort_name'   ,
                                 ts('Address')      => 'address'     ,
                                 ts('City')         => 'city'        ,
                                 ts('ZIP')          => 'postal_code' ,
                                 ts('Email')        => 'email'       ,
                                 ts('Label1')  => 'field1' ,
                                 ts('Label2')  => 'field2' ,
                                 ts('Label3')  => 'field3' );
        ...
        $selectClause = "
                contact_a.id              as contact_id ,
                contact_a.sort_name       as sort_name  ,
                address.street_address    as address    ,
                address.city              as city       ,
                address.postal_code       as postal_code,
                email.email               as email      ,
                custom_group.fieldID1 as field1,
                custom_group.fieldID2 as field2,
                custom_group.fieldID3 as field3
        ";

From the working queries (there are 13 different where clauses, one per query, all of which work):

        $where  = "LENGTH(custom_group.fieldID1) > 1";
                - or -
        $where  = "LENGTH(custom_group.fieldID1) > 4";
                - or -
        $where  = "LENGTH(custom_group.fieldID1) > 7";
                - or -
        $where  = "LENGTH(custom_group.fieldID1) > 10";
                - or -
        $where  = "LENGTH(custom_group.fieldID2) > 1";
                - or -
        $where  = "LENGTH(custom_group.fieldID2) > 4";
                - or -
        $where  = "LENGTH(custom_group.fieldID2) > 7";
                - or -
        $where  = "LENGTH(custom_group.fieldID2) > 10";
                - or -
        $where  = "LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)=0 AND LENGTH(custom_group.fieldID3)>0";
                - or -
        $where  = "(LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)=0 AND LENGTH(custom_group.fieldID3)>0) OR (LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)>0)";
                - or -
        $where  = "(LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)=0 AND LENGTH(custom_group.fieldID3)>0) OR (LENGTH(custom_group.fieldID1)>0 AND LENGTH(custom_group.fieldID2)=0)";
                - or -
        $where  = "LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)=0 AND custom_group.fieldID3 NOT LIKE '%05%' AND custom_group.fieldID3 NOT LIKE '%07%' AND custom_group.fieldID3 NOT LIKE '%09%' AND custom_group.fieldID3 NOT LIKE '%11%' AND custom_group.fieldID3 NOT LIKE '%13%'";
                - or -
        $where  = "LENGTH(custom_group.fieldID1)=0 AND LENGTH(custom_group.fieldID2)=0 AND custom_group.fieldID3 NOT LIKE '%02%' AND custom_group.fieldID3 NOT LIKE '%03%' AND custom_group.fieldID3 NOT LIKE '%05%' AND custom_group.fieldID3 NOT LIKE '%06%' AND custom_group.fieldID3 NOT LIKE '%07%' AND custom_group.fieldID3 NOT LIKE '%09%' AND custom_group.fieldID3 NOT LIKE '%10%' AND custom_group.fieldID3 NOT LIKE '%11%'";

From the one no-longer-working query:

        $where  = "((custom_group.fieldID3 LIKE '%05%') OR (custom_group.fieldID3 LIKE '%07%') OR (custom_group.fieldID3 LIKE '%09%') OR (custom_group.fieldID3 LIKE '%11%') OR (custom_group.fieldID3 LIKE '%13%'))";
« Last Edit: June 05, 2013, 08:48:31 pm by Erich »

Erich

  • I post occasionally
  • **
  • Posts: 84
  • Karma: 1
Re: Custom Queries no longer working
June 06, 2013, 06:46:02 am
Found the problem.  While I *am* using a Civi custom search, the problem *did* turn out to not be a Civi question, but a problem with the format of the imported data.  Fixing the data also fixed the query results.

D'oh.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Custom Queries no longer working (RESOLVED)

This forum was archived on 2017-11-26.