croxton / Search_fields

Search channel entry titles, custom fields, category names, category descriptions and category custom fields
20 stars 9 forks source link

Multiple category custom fields #9

Open willmcclellan opened 12 years ago

willmcclellan commented 12 years ago

Hi Mark,

Does the plugin only work with one custom category field? I have 5 listed in the parmeters and it only seems to work with first one listed. e.g:

{exp:search_fields
    search:title="{embed:search_keyword}"
    search:product_id="{embed:search_keyword}"
    search:cat_name="{embed:search_keyword}"
    search:cat_description="{embed:search_keyword}"
    search:cat_search_terms_women="{embed:search_keyword}"
    search:cat_search_terms_men="{embed:search_keyword}"
    search:cat_search_terms_girls="{embed:search_keyword}"
    search:cat_search_terms_boys="{embed:search_keyword}"
    search:cat_search_terms_brands="{embed:search_keyword}"
    operator="OR"
    channel="catalogue"
    parse="inward"
}
croxton commented 12 years ago

It does work with multiple fields (as I recall). Can you please turn on the database debugger and post the query that is generated?

willmcclellan commented 12 years ago

Ah ok, here's the query:

SELECT distinct(wt.entry_id)
        FROM exp_channel_titles AS wt
        LEFT JOIN exp_channel_data AS wd
        ON wt.entry_id = wd.entry_id
        LEFT JOIN exp_channels AS wl
        ON wt.channel_id = wl.channel_id
        LEFT JOIN exp_category_posts as cp
                    ON wt.entry_id = cp.entry_id
                    LEFT JOIN exp_categories as ct
                    ON cp.cat_id = ct.cat_id
                    LEFT JOIN exp_category_field_data as cd
                ON ct.cat_id = cd.cat_id
                WHERE wt.site_id = 1 
AND ((  ( wt.title LIKE "%my search query%" )  OR ( wd.field_id_4 LIKE "%my search query%" )  OR ( ct.cat_name LIKE "%my search query%" )  OR ( ct.cat_description LIKE "%my search query%" )  OR ( ct.cat_description LIKE "%my search query%" )  OR ( ct.cat_description LIKE "%my search query%" )  OR ( cd.field_id_1 LIKE "%my search query%" )  OR ( cd.field_id_1 LIKE "%my search query%" )  OR ( cd.field_id_1 LIKE "%my search query%" ) ) AND wl.channel_name = 'catalogue') 

It seems to be just looking in field_id_1 whatever order I put the parameters in.

croxton commented 12 years ago

On line 145 please can you add

print_r($this->_cat_fields); die();

View source and let me know what is output to the screen.

willmcclellan commented 12 years ago

I get this:

Array
(
    [1] => Array
        (
            [search_terms_women] => 1
        )

)

which is the name of field_id_1 in my category custom fields. This shows whatever order I list the cat_custom_field params

croxton commented 12 years ago

Spotted the problem.

In private function _fetch_custom_category_fields() please change this block of code:

       foreach ($query->result_array() as $row)
       {
        // assign standard fields
            $this->_cat_fields[$row['site_id']][$row['field_name']] = $row['field_id'];
        return true;
       }
       $this->EE->session->cache['search_fields']['custom_category_fields'] = $this->_cat_fields;

To:

       foreach ($query->result_array() as $row)
       {
        // assign standard fields
            $this->_cat_fields[$row['site_id']][$row['field_name']] = $row['field_id'];
       }
       $this->EE->session->cache['search_fields']['custom_category_fields'] = $this->_cat_fields;
       return true;
willmcclellan commented 12 years ago

That's sorted it Mark. Thanks so much for your help with that. Much appreciated!

Will