Hi, can anyone tell me if it’s possible to use the include() function to query multiple reference fields ? And if so, can you please tell me what I’m doing wrong?
I have two tables with a many-to-many relationship and I really need to be able to actually query Table A by references to Table B.
Example : Table A contains event photos, Table B contains categories. I have a query that runs when users enter search terms. If a user enters a category (ex. weddings), any event photos with the term “weddings” in it’s category reference field would be returned.
I’ve searched all over and I can’t find an answer as to whether or not it’s actually possible to use the include() function in this way, but it seems to be my best bet. From what I can tell, you should be able to use it if a collection has only one multiple reference field , but I can’t get it to work.
I’d really appreciated some help with this - this is the one thing that’s completely holding me up with using data collections in my site.
Here is what I have tried so far to accomplish a query on a multiple reference field:
Using the Include() function in my query to include the reference field
Concatenating my results, which I don’t think is really what I’m going for
Using hooks to combine fields and create one long string of searchable terms, including those found in the reference field - however, reference fields only appear as “Object” when they are copied to a text field.
Here’s the relevant code, with my current efforts to use Include():
export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”) <— Table A (EventPhotos)
.include(“categories”) <— reference field to Table B (Categories)
.contains(“relatedjob”, $w(“#searchbox”).value)
.or(wixData.query(“EventPhotos”)
.contains(“jobdescription”, $w(“#searchbox”).value))
.or(wixData.query(“EventPhotos”)
.contains(“categories”, $w(“#searchbox”).value))
So, where exactly in my code would I use that? Would I still need to have a separate query for the event photos themselves? Or would I replace my current query with the query referenced function?
@yoav-wix Okay, meaning that I would populate my results repeater with each event photos’ referenced categories? But then will that allow users to search both by category AND by other fields in the event photos database? (Such as description or name).
@yoav-wix Hi, I’m still struggling with this… I don’t know much about java at all, obviously. Can you please just show me an example of how I should utilize the queryreferenced function within my code? I would REALLY appreciate it.
queryReferenced( )
function queryReferenced(collectionName: string, item: Object | string, propertyName: string, options: WixDataQueryReferencedOptions): Promise<WixDataQueryReferencedResult>
...
item Object | string
The referring item or referring item's ID.
that you can use only the referring item’s ID, you can’t use the item text.
At least I so understood, but it’s not so clera the differences with include.
Anyway