How to set > operator in the andFilterWhere - yii2

How to correctly add into the andFilterWhere method more or less operand?
andFilterWhere(['user_id' => 1]) - here I need to add > operand

If the argument you pass into andFilterWhere is an array with 0-based indexes the first item in array is treated as operator and others as operands.
So if you want to use > comparison you can do it like this:
andFilterWhere(['>', 'user_id', 1])
See the documentation for yii\db\QueryInterface::where() for more details.

Related

can't understand the syntax of [lsort [::array names my_array *,dut_inst]]

I can't understand this:
[lsort [::array names my_array *,dut_inst]]
What meaning of * ?
What meaning of dut_inst ?
Where can I read about it ?
Let's assume:
my_array(0)=0
my_array(1)=1
my_array(2)=2
What will I get ?
That optional argument to array names is an optional glob pattern that is used to filter the results to return down to a subset. The rules for how it works are described in the documentation for string match, but in the case of *,dut_inst we have two parts:
* matches any number of characters.
,dut_inst is literal (as none of the characters in it are special in the string match rules).
The effect is to return a list of all element names whose names end with ,dut_inst. With your sample data, you get an empty list. With this sample data:
my_array(foo,bar)=1
my_array(boo,dut_inst)=2
my_array(dut_inst,grill)=3
my_array(abc,dut_inst,def)=4
my_array(pqr,dut_inst)=5
You'd get this output (assuming the lsort is there; Tcl does not guarantee the order of array iteration): boo,dut_inst pqr,dut_inst

How can I avoid autocoercion in Couchbase queries?

An N1QL query has a filter WHERE myField < $value.
From experimenting, I see that Couchbase orders the types as follows: boolean < integer < string < JsonArray, even though from my perspective they should not be comparable.
For example, any boolean evaluates as less than any integer; any integer is less than all strings. (9223372036854775807 (Long.MAX_VALUE) evaluates as less than "" (empty string).)
I want to avoid this type-coercion. I want "A" < 1 and "A" > 1 to be false and not to return such values from the filter. (And also, it seems that in Javascript, both these expressions are false, as they should be.)
What are the coercion rules? How do I prevent this?
You have discovered the collation order of N1QL. Here is a fuller explanation:
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/datatypes.html
If you want to avoid this comparison across types, you can add a clause using the TYPE() function to verify that the two elements being compared are of the same type.
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/typefun.html
So rather than having $A > 3 you would have ($A > 3) AND (TYPE($A) = TYPE(3)).

How to test if collection contains all elements of other collection

With a Set in Ceylon it is straightforward to determine if one collection is a superset of the other. It's just first.superset(second). What's the best way to do the equivalent for an Iterable, List, or Sequential using multiset (or bag) semantics? For example something like the pseudocode below:
{'a', 'b', 'b', 'c'}.containsAll({'b', 'a'}) // Should be true
{'a', 'b', 'b', 'c'}.containsAll({'a', 'a'}) // Should be false
There is Category.containsEvery, which is inherited by Iterable. It checks for each element of the parameter whether it is contained in the receiver, so that bigger.containsEvery(smaller) is equivalent to this:
smaller.every(bigger.contains)
(Note that it is swapped around.) The expression in the brackets here is a method reference, we could also write this expanded with a lambda:
smaller.every(o => bigger.contains(o))
So in your example:
print({'a', 'b', 'b'}.containsEvery({'b', 'a'})); // Should be true
print({'a', 'b', 'b'}.containsEvery({'a', 'a'})); // Should be false
... actually, those both return true. Why do you think the latter one is false?
Did you think of multiset semantics (i.e. the number of occurrences in the "superset" iterable need to be at least as much as the smaller one)? Or do you want a sublist? Or do you just want to know whether the second iterable is at the start of the first (startswith)?
I don't know about any multiset implementation for Ceylon (I found a multimap, though). If you are running on the JVM, you can use any Java one, like from Guava (though that also doesn't have a "contains all with multiples" function, as far as I can see).
For small iterables, you can use .frequencies() and then compare the numbers:
Boolean isSuperMultiset<Element>({Element*} bigger,
{Element*} smaller) =>
let (bigFreq = bigger.frequencies())
every({ for(key->count in smaller.frequencies())
count <= (bigFreq[key] else 0) })
For sublist semantics, the SearchableList interface has the includes method, which checks whether another list is a sublist. (It is not implemented by many classes, though, you would need to convert your first iterable into an Array, assuming it is not a String/StringBuilder.)
For startsWith semantics, you could convert both to lists and use then List.startsWith. There should be a more efficient way of doing that (you just could go through both iterators in parallel).
There is corresponding, but it just stops after the shorter one ends (i.e. it answers the question "does any of those two iterables start with the other", without telling which one is the longer one). Same for a bunch of other pair related functions in ceylon.language.
If you know the length of both of the Iterables (or are confident that .size is fast), that should solve the issue:
Boolean startsWith<Element>({Element*}longer, {Element*}shorter) =>
shorter.size <= longer.size &&
corresponding(longer, shorter);
If you have two Sequentials, then you can remove each right-hand character one at a time from the left-hand sequence until you either remove them all or fail to remove one of them.
Boolean containsAll<Element>([Element*] collection, [Element*] other)
given Element satisfies Object {
variable value remaining = collection;
for (element1 in other) {
value position = remaining.locate((element2) => element1 == element2);
if (exists position) {
remaining = remaining.initial(position.key).append(remaining.spanFrom(position.key + 1));
} else {
// Element was not found in remaining; terminate early
return false;
}
}
// All elements were found
return true;
}
print(containsAll(['a', 'b', 'b', 'c'], ['a', 'b']));
print(containsAll(['a', 'b', 'b', 'c'], ['a', 'a']));
Append only exists on Sequential so it won't work on just a List or an Iterable.
The containsEvery function should do what you want (try it!). Alternatively, you can also turn both streams into sets using the set function (try it!), or use every and contains (try it!).

multiple entity type in entity field query

How to add multiple entity_type and bundles in EntityFieldQuery?
$query->entityCondition('entity_type', 'profile2')
->entityCondition('bundle', 'user_profile');
Is there any way to do like this?
$query->entityCondition('entity_type', array('profile2','user'))
->entityCondition('bundle', array('user_profile','property'));
This isn't possible: https://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityFieldQuery/7
This class allows finding entities based on entity properties (for example, node->changed), field values, and generic entity meta data (bundle, entity type, entity id, and revision ID). It is not possible to query across multiple entity types. For example, there is no facility to find published nodes written by users created in the last hour, as this would require querying both node->status and user->created.
In the official doc:
public EntityFieldQuery::entityCondition($name, $value, $operator = NULL)
where:
$name: 'entity_type', 'bundle', 'revision_id' or 'entity_id'.
$value: The value for $name. In most cases, this is a scalar. For more complex options, it is an array. The meaning of each element in the array is dependent on $operator.
$operator: Possible values:
'=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These operators expect $value to be a literal of the same type as the column.
'IN', 'NOT IN': These operators expect $value to be an array of literals of the same type as the column.
'BETWEEN': This operator expects $value to be an array of two literals of the same type as the column.
I guess you forgot the operator!

What's the difference between Where(lambda expr.) and First(lambda expr.) when I need to pick the first item?

I'd like to know the difference between this two syntaxes:
return db.Contacts.First(x => x.ContactID == id)
That I've been using so far until I get an error "Sequence contains no elements". then I have to use the below one again.
return db.Contacts.Where(x => x.ContactID == id).First();
There must be a nuance I'm not getting right.
Thanks for helping.
First is used to get the first element of a sequence, but it also takes a predicate to allow you to filter the IEnumerable sequence.
So First will return the first element of an IEnumerable with 1 or more results.
First(Predicate) will return the first element of results within an IEnumerable with 1 or more results.
If there are 0 elements when using First you'll get the error Sequence contains no elements. If you're expecting there to be 0 results, you should use FirstOrDefault. FirstOrDefault can also take a predicate as well, i.e.
// Will return null if there are no elements with a matching contact Id
return db.Contacts.FirstOrDefault(x => x.ContactID == id);