My code look like this:
$database->update($campaign_table, $data_nga_posti , array("AND" => ["codice_pod" => $data_nga_posti['codice_pod'],("codice_pdr" =>NULL) ]));
So this is when my query execute:
UPDATE `field` SET `columnname` = 'data',`anothercm` = 'data',WHERE `codice_pod` = 'IT001E35563561' AND `codice_pdr` IS NULL
I want to my query look like this.
UPDATE `field` SET `columnname` = 'data',`anothercm` = 'data',WHERE `codice_pod` = 'IT001E35563561' AND (`codice_pdr` IS NULL OR `codice_pdr` ="")
but I dont know how to put OR(operator ) inside this code.
I have never used this before, and have nothing to test with but the closted example posted in the documentation is:
$database->has("account", [
"AND" => [
"OR" => [
"user_name" => "foo",
"email" => "foo#bar.com"
],
"password" => "12345"
]
]);
So I think this would work for you:
$database->update($campaign_table, $data_nga_posti, [
"AND" => [
OR => [
"codice_pdr" =>NULL,
"codice_pdr" => ""
],
"codice_pod" => $data_nga_posti['codice_pod']
]
]);
try this:
array("AND" => ["codice_pod" => $data_nga_posti['codice_pod'],"OR" => ["codice_pdr" =>NULL,`codice_pdr` ="" ] ])
Related
I have this code and its working:
...
],
(
($post['categories']['show'] ?
[
'name' => "categories",
'data' => ['categories' => self::getCategories($post)]
] : ''
),
[
...
However if its false its returning "" on the output but if its false it should not return nothing it should not show nothing. Do you know how to achieve that? With : null, it shows null, instead oof "",.
I also tried like this but don't works:
$result = [
'components' => [
[
...
],
if($post['categories']['show'])
{
[
'name' => "categories",
'data' => ['categories' => self::getCategories($post)]
];
}
....
];
return $result;
}
The whole code is really big. But it follows that structure array then ",".
Don't use an empty string for the else conditional, use null, then use array_filter, it will clean all null values of the array for you:
$result = [
$condition ? 'bar' : null,
'foobaz',
...
];
// return value will be: ["foobaz"]
return array_filter($result);
Having this document structure in MongoDB :
{
"_id":<MongoDBID>,
"chatUser1ID": 2,
"chatUser2ID": 3
}
Now i want to get all chat partners from Mongo where the Chat Partner with the ID 2 is included in either "chatUser1" or "chatUser2". For that i want to use the $match and $group function.
$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
[
'$match' =>
[
'$or' =>
[
["chatUser1ID" => $chatUserID],
["chatUser2ID" => $chatUserID]
]
]
]
,[
'$group' =>
[
'_id' => 0,
'chatUsers' => ['$addToSet' => '$chatUser1ID'],
'chatUsers' => ['$addToSet' => '$chatUser2ID'],
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
]
]
]);
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
This is putting all the ID's of the field chatUser1ID in the chatUsers1 set and
'chatUsers2' => ['$addToSet' => '$chatUser2ID']
is putting all all the ID's of the field chatUser2ID in the chatUsers2 set where the $chatUserID is either in the chatUser1ID or chatUser2ID field of the document.
After that i want want to get the unique ID's
$chatUserIDs = array();
foreach ($chatCursor as $counter => $document) {
$bson = MongoDB\BSON\fromPHP($document);
$value = MongoDB\BSON\toJSON($bson);
$value = json_decode($value, true);
$chatUserIDs = array_unique(array_merge($value['chatUsers1'], $value['chatUsers2']));
}
unset($chatUserIDs[array_search($chatUserID, $chatUserIDs)]);
array_unshift($chatUserIDs);
So basically it's working but i want the solution where i get a list of unique ID's right away from the database.
Originally i thought the lines
'chatUsers' => ['$addToSet' => '$chatUser1ID'],
'chatUsers' => ['$addToSet' => '$chatUser2ID'],
would add the ID's to the set chatUsers but unfortunately the set is overwritten in the second line. Is there a way to "append" the ID's to the set instead of overwrite them ? And maybe a way where i can exclude the $chatUserID because i want only the chatPartners.
Thanks in advance.
If you don't care about the order they appear in, you can build two arrays of user1 and user2, then concat them together in a later stage. This won't handle deduplicating though.
$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
[
'$match' => [
'$or' =>[
["chatUser1ID" => $chatUserID],
["chatUser2ID" => $chatUserID]
]
]
], [
'$group' => [
'_id' => 0,
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
]
], [
'$addFields' => [
'chatUsers' => [
'$concatArrays' => [
'$chatUsers1',
'$chatUsers2'
]
]
]
],
]);
I have been using Elasticsearch 7.6 and PHP client API for all the operations.
I have created elasticsearch index settings and mappings as follows
$params = [
'index' => 'elasticindex',
'body' => [
'settings' => [
"number_of_shards" => 1,
"number_of_replicas" => 0,
"index.queries.cache.enabled" => false,
"index.soft_deletes.enabled" => false,
"index.requests.cache.enable" => false,
"index.refresh_interval" => -1
],
'mappings' => [
'_source' => [
"enabled" => false
],
'properties' => [
"text" => [
"type" => "text",
"index_options" => "docs"
]
]
]
]
];
I was able to index document using the following code
$params = array();
$params['index'] = 'elasticindex';
for($i = 1; $i <=2; $i++) {
$params['id'] = $i;
$params['body']['text'] = 'apple';
$responses = $client->index($params);
}
But when I use the following search query
$params = [
'index' => 'elasticindex',
'body' => [
'query' => [
'match' => [
"text" => "apple"
]
]
]
];
$results = $client->search($params);
I am getting empty results as follows
Array
(
[took] => 3
[timed_out] =>
[_shards] => Array
(
[total] => 1
[successful] => 1
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => Array
(
[value] => 0
[relation] => eq
)
[max_score] =>
[hits] => Array
(
)
)
)
Without creating a static index template, if I try to index, elasticsearch dynamic mapping works well and I am getting the results.
The goal is that I want the elasticsearch to index only document id in its inverted index and not position or offset and I want to retrieve only matching document ids as results. Help is much appreciated. Thanks in advance!
Since the document is being returned by the get handler and not the query handler, your index is not being refreshed properly after indexing the document.
As you noted yourself, in your configuration you set:
"index.refresh_interval" => -1
.. which means that the index is not being refreshed automagically. There's seldom a need to change the refresh interval, except in very high throughput situations or where a particular behavior is wanted.
Try to index doc something like this.
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'elasticindex',
'type' => 'documents',
'id' => '1',
'body' => ['text' => 'apple']
];
$response = $client->index($params);
print_r($response);
Note: _id you can define dynamically if you want, else id will automatically set by elastic.
And try to get doc via a search query.
{
"query": {
"bool": {
"must": {
"term": {
"text": "apple"
}
}
}
}
}
If you want full-text search on this key, set this key property as
"properties": {
"name": {
"type": "text"
}
}
I want to compare two columns of medoo rather its values.
$issues = $database->select("issues","*",[
"AND" => [
"projectid" => 1,
"estimated_hrs[<]" => "timespent"
]
]);
But getting an error while doing so.Is there any solution??
Check out columns relationship here: https://medoo.in/api/where
$database->select("post", [
"[>]account" => ["author_id" => "user_id"],
], [
"post.id",
"post.content"
], [
"AND" => [
// Connect two column with condition sign like [=], [>], [<], [!=] as one of array value
"post.restrict[<]account.age",
"account.user_name" => "foo",
"account.email" => "foo#bar.com",
]
]
);
// WHERE "post"."restrict" < "account"."age" AND "account"."user_name" = 'foo' AND "account"."email" = 'foo#bar.com'
I have below array, I need to append a new array inside $newData['_embedded']['settings']['web/vacation/filters']['data'], How can I access and append inside it ?
$newData = [
"id" => "47964173",
"email" => "abced#gmail.com",
"firstName" => "Muhammad",
"lastName" => "Taqi",
"type" => "employee",
"_embedded" => [
"settings" => [
[
"alias" => "web/essentials",
"data" => [],
"dateUpdated" => "2017-08-16T08:54:11Z"
],
[
"alias" => "web/personalization",
"data" => [],
"dateUpdated" => "2016-07-14T10:31:46Z"
],
[
"alias" => "wizard/login",
"data" => [],
"dateUpdated" => "2016-09-26T07:56:43Z"
],
[
"alias" => "web/vacation/filters",
"data" => [
"test" => [
"type" => "teams",
"value" => [
0 => "09b285ec-7687-fc95-2630-82d321764ea7",
1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
],
],
"multiple teams" => [
"type" => "teams",
"value" => [
0 => "359c0f53-c9c3-3f88-87e3-aa9ec2748313"
]
]
],
"dateUpdated" => "2017-07-03T09:10:36Z"
],
[
"alias" => "web/vacation/state",
"data" => [],
"dateUpdated" => "2016-12-08T06:58:57Z"
]
]
]
];
$newData['_embedded']['settings']['web/vacation/filters']['data'] = $newArray;
Any Hint to quickly append it, I don't want to loop-in and check for keys inside loops.
The settings subarray is "indexed". You first need to search the alias column of the subarray for web/vacation/filters to find the correct index. Using a foreach loop without a break will mean your code will continue to iterate even after the index is found (bad coding practice).
There is a cleaner way that avoids a loop & condition & break, use array_search(array_column()). It will seek your associative element, return the index, and immediately stop seeking.
You can use the + operator to add the new data to the subarray. This avoids calling a function like array_merge().
Code: (Demo)
if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
$newData['_embedded']['settings'][$index]['data']+=$newArray;
}
var_export($newData);
Perhaps a more considered process would be to force the insert of the new data when the search returns no match, rather than just flagging the process as unsuccessful. You may have to tweak the date generation for your specific timezone or whatever... (Demo Link)
$newArray=["test2"=>[
"type" =>"teams2",
"value" => [
0 => "09b285ec-7687-fc95-2630-82d321764ea7",
1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
],
]
];
if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
//echo $index;
$newData['_embedded']['settings'][$index]['data']+=$newArray;
}else{
//echo "couldn't find index, inserting new subarray";
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC')); // or whatever you are using
$stamp=$dt->format('Y-m-d\TH-i-s\Z');
$newData['_embedded']['settings'][]=[
"alias" => "web/vacation/filters",
"data" => $newArray,
"dateUpdated" => $stamp
];
}
You need to find the key that corresponds to web/vacation/filters. For Example you could use this.
foreach ($newData['_embedded']['settings'] as $key => $value) {
if ($value["alias"]==='web/vacation/filters') {
$indexOfWVF = $key;
}
}
$newData['_embedded']['settings'][$indexOfWVF]['data'][] = $newArray;
From the comments. Then you want to merge the arrays. Not append them.
$newData['_embedded']['settings'][$indexOfWVF]['data'] = array_merge($newData['_embedded']['settings'][$indexOfWVF]['data'],$newArray);
Or (if it's always Filter1):
$newData['_embedded']['settings'][$indexOfWVF]['data']['Filter1'] = $newArray['Filter1'];