How to filter Sanity posts by category title? - sanity

Here what I've done in vision
*[_type == "post" && categories == SOCIAL ]{
_id, title
}
It returned
No documents found in dataset production that match query:
*[_type == "post" && categories == SOCIAL ]{
_id, title
}

You have to format it like so:
*[_type == "post" && categories == "SOCIAL" in categories[]->title]{
title,
slug,
body
}
If nothing shows up then there are no posts associated with that category. Categories are also case sensitive, so make sure your capitalization is right.

*[_type == "post" && "social" in categories[]->title]{
title
}

*[_type == 'post' && category->name == 'Technology']
Can also try this in cases where your categories are not saved as arrays.

What worked for me is instead of searching by the name of the category i searched using the ._ref property of the category.
It would look something like this
*[_type == "post" && categories == categories._ref]

Related

How to fetch multiple documents within a single query?

I want to fetch multiple pages within a single query.
My code is causing errors
const pagesQuery = groq`{
*[_type == 'homepage']{slug},
*[_type == 'faq']{slug},
*[_type == 'contact']{slug}
}`
Something like this should work.
*[_type in ["homepage", "faq", "contact"]]{slug}

Condition in kendo grid ClientTemplate

I am using kendo grid in asp.net mvc4. A column of a grid is Email, so I make a link to the column like that:
col.Bound(con => con.EmailName).Title("Email").Width(120).ClientTemplate(" #=EmailName == null ? '': EmailName#").Filterable(true);
It's Working well. But, I also have to field DoNotEmail and DoNotEmailMarketingCampaigns Whose are Boolean. I want to make the link if both field are false, otherwise I will not make a link. I tried following way:
col.Bound(con => con.EmailName).Title("Email").Width(120).ClientTemplate("#if(!DoNotEmailMarketingCampaigns && !DoNotEmail) { # #=EmailName == null ? '': EmailName# #} else { #=EmailName == null ? '': EmailName# } #").Filterable(true);
but it's not serving my purpose. Any suggestions?
Finally Done with following Code:
col.Bound(con => con.EmailName).Title("Email").Width(120).ClientTemplate("#if(!DoNotEmailMarketingCampaigns && !DoNotEmail) { # #=EmailName == null ? '': EmailName# #} else {# #=EmailName == null ? '': EmailName# #} #").Filterable(true);

how to write "select * from PUB_PAGE_MENUSTRUCTUR where PARENTID is null" using lambda

This is my lambda to get the data from pub_page_menustructur
DataHelper.DataObj.QueryTable(SystemType.H0, p=>p.PARENTID == null) this is the lambda expression i had write. it have the same effection as "select * from pub_page_menustructur where parentid = null" show in the picture. is there any other way to show as "parentid is null"
This way work for you if you are using Entity Framework
var result = dbcontext.PUB_PAGE_MANUSTRUCTURE.Where(w => w.PARENTID == null).ToList();
the answer is
var result = dbcontext.PUB_PAGE_MANUSTRUCTURE.Where(w => w.PARENTID.Trim() == null).ToList();

Grails: "where" query with optional associations

I'm trying to run a "where" query to find a domain model object that has no association with another domain model object or if it does, that domain model object has a specific property value. Here's my code:
query = Model.where({
other == null || other.something == value
})
def list = query.list()
However, the resulting list only contains objects that match the second part of the OR statement. It contains no results that match the "other == null" part. My guess is that since it's checking a value in the associated object its forcing it to only check entries that actually have this associated object. If that is the case, how do I go about creating this query and actually having it work correctly?
You have to use a LEFT JOIN in order to look for null associations. By default Grails uses inner join which will not be joined for null results. Using withCriteria as below you should get the expected results:
import org.hibernate.criterion.CriteriaSpecification
def results = Model.withCriteria {
other(CriteriaSpecification.LEFT_JOIN){
or{
isNull 'id'
eq 'something', value
}
}
}
UPDATE
I know aliasing is not possible in DetachedCritieria where one would try to specify the join as in createCriteria/withCriteria. There is an existing defect regarding adding the functionality to DetachedCriteria. Just adding the work around for where query as mentioned in defect.
Model.where {
other {
id == null || something == value
}
}.withPopulatedQuery(null, null){ query ->
query.#criteria.subcriteriaList[0].joinType = CriteriaSpecification.LEFT_JOIN
query.list()
}
I would rather use withCriteria instead of the above hack.
this might work:
query = Model.where({
isNull( other ) || other.something == value
})
If that wouldn't work, try something like:
other.id == null || other.something == value
UPDATE:
or with good'ol criteria query:
list = Pack.withCriteria{
or{
isNull 'other'
other{ eq 'something', value }
}
}

How to select items by providing where clause in pivot

This is my code looks like right now :
$result = Category::with(array(
"getProducts",
"getProducts.getBrand",
"getProducts.getImages",
"getProducts.getDetail",
"getProducts.getTax",
"getProducts.getDiscount",
"getProducts.getAttributes" => function($query) use($_temp){
if($_temp != null){
$query->where_in('attributes.id',$_temp);
}
},
"getSlideshow",
"getSlideshow.getItems",
"getAttributeListing",
"getAttributeListing.getAttributes",
"getAttributeListing.getAttributes.productSpecific"
))
->where('id','=', $category_id)
->first();
Yet, It only filters the getProducts.getAttributes items not the 'getProducts' itself. Is there a way that I can get the Products by attributes?
Note: I am using Laravel 3