Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Querying Multiselect Fields View modes: 
User avatar
Member
Member
Armysniper89 - 10/15/2011 11:08:40 PM
   
Querying Multiselect Fields
In a custom table, when you have multiple selection fields, Kentico allows you to store them as piped indexes into a lookup table. For example, if I had a custom event table and one of my fields allowed the creator of the event the ability to choose multiple age groups that may be interested in the event such as toddlers, adults, teens, etc. So an event might have in its age group field multiple age groups that an event matches to. So it might have a value of "1|7|10" stored as a string, not a single integer.

I have to create a search filter. When a user who is looking for events enters in lets say "toddler" as an age group which is index 1, I want to search all records that have "1" for a value of the age groups field...but an event might have "1|7|10" as a value for "age group". How can I query that if I have single integer index field to search for...yet the index is stored in a field that contains a string of "index" values?

Also, if I want to inner join on that field with the lookup table...how can I so I can display in a grid or transformation the word..."Toddler" from the "Age Group" look up table when the event has multiple indexes stored as a string? I dont know how to inner join if the index from the event table is a string of indexes.

Any help would be great...

User avatar
Member
Member
kentico_michal - 10/16/2011 3:19:35 AM
   
RE:Querying Multiselect Fields
Hello,

In order to achieve this, you will need to use the CustomDataHandler class and in particular, its OnGetContent method. In general, the custom handler (CustomDataHandler, CustomTreeNodeHandler etc.) gives you ability to execute custom code when some CMS event occurs. The events of CustomDataHandler are applied to all data items that are stored to the database and the OnGetContent is a method used by smart search. It's called every time the document (or custom table, forum, user) is indexed.

The first parameter of the OnGetContent method usually contains a TreeNode object and the second parameter contains text that gets indexed by Lucene.NET search engine used in Kentico CMS smart search module (let's call it content). In this case, you will need get the Multiple choice field of the currently idexed document, parse it to get IDs of all fields, get appropriate records from Age Group based on IDs and add them to the content variable.

Let's assume that Age Group is a custom table and you use the following query to bind the Mupliple choice field:

SELECT ItemID, AgeGroup from customtable_AgeGroup

For this scenario, the OnGetContent method could look like this one:

public override string OnGetContent(object obj, string content)
{
if (obj is TreeNode)
{
TreeNode node = obj as TreeNode;
if (node != null)
{
string field = ValidationHelper.GetString(node.GetValue(<Multiple choice field)>, String.Empty);
if ( !String.IsNullOrEmpty(field) )
{
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);

string customTableClassName = "customtable.agegroup";

string [] ids = field.Split('|');
foreach (string id in ids)
{
CustomTableItem item = customTableProvider.GetItem(ValidationHelper.GetInteger(id, 0), customTableClassName);

content = content + " " + CMS.GlobalHelper.ValidationHelper.GetString(item.GetValue("AgeGroup"), "");
}
}
}
}
return content;
}


Now, you should be able to search for documents using values form the AgeGroup table (toddler etc.).

If you want more information about the CustomDataHandler class and how to add it to your project, please visit following links:
Event handling overview
Data handler

In terms of displaying correct values in transformation, you need to create a custom transformation method:
Adding custom functions to transformations

Another example of using OnGetContent which might help you understand what you need to do to make it work can be found in this knowledge base article: How to search for documents using assigned category

Best regards,
Michal Legen

User avatar
Member
Member
Armysniper89 - 10/16/2011 8:52:14 AM
   
RE:Querying Multiselect Fields
Thanks for the thorough Kentico solution but I am building a custom web part and was hoping for a more pure SQL solution. I posted it here figuring that someone must have run into this issue since it is slightly different than a standard CSV list of indexes.

User avatar
Member
Member
kentico_michal - 10/18/2011 1:40:18 AM
   
RE:Querying Multiselect Fields
Hello,

Well, I am not sure if we have had a similar request so far. However, you might find following articles useful:
Inner join with comma separated values, SQL - Best split functions


Best regards,
Michal Legen

User avatar
Member
Member
mnavarro-tiempodevelopment - 3/25/2013 4:40:22 PM
   
RE:Querying Multiselect Fields
Hi Michal.

I'm trying to split the values to perform other select in the parent table but query in custom tables does not support variable declarations.

What can I do?

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 4/3/2013 8:23:09 AM
   
RE:Querying Multiselect Fields
Hello,

Just FYI, you're replying to a little bit dated discussion.
Nevertheless, could you please describe your aim in more details? Maybe there's another way too...

Thank you in advance for information.
Regards,
Zdenek