updateSingleValue() returns false, yet inputs are correct - bolt-cms

I'm using updateSingleValue like this:
$updated = $this->app['storage']->updateSingleValue('suggestions', $id, 'votes', $value);
The $id and $value are set correctly (even if I set them to correct integers manually), and there definitely exists a suggestions contenttype with votes field. Yet, this function returns false, suggesting that !$this->isValidColumn($field, $contenttype) fails.
A suggestion in json format looks like this:
suggestion": {
"id": "25",
"values": {
"datechanged": "2015-01-03 13:25:02",
"id": "25",
"slug": "slug-o2sbdb",
"datecreated": "2015-01-03 13:25:02",
"datepublish": "2015-01-03 13:25:02",
"datedepublish": "1900-01-01 00:00:00",
"ownerid": "1",
"status": "published",
"title": "test title",
"description": "test description",
"votes": "0"
},
The suggestion contenttype looks like this:
suggestions:
name: Suggestions
slug: suggestions
singular_name: Suggestion
singular_slug: suggestion
default_status: publish
fields:
title:
label: Title
type: text
class: large
required: true
pattern: ".{2,255}" # see: http://html5pattern.com/
error: "The Title field is required, and must contain at least 2 characters"
description:
label: Description
type: textarea
votes:
label: Votes
type: integer
What can I be doing wrong here?

Found the problem here the contenttype passed into updateSingleValue needs to be the entire contenttype not just the slug 'suggestions'. You can fetch it via the storage class method:
$app['storage']->getContentType('suggestions')
then pass in the result of that to the updateSingleValue method.

Related

Sort by nested fields in json in Power Automate

I'm trying to sort a JSON String in Power Automate by a nested field called "orderHint".
My JSON String looks like this:
[
{
"id": "5134",
"value": {
"isChecked": false,
"title": "This is another test",
"orderHint": "8585298133570680672PF"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
},
{
"id": "26576",
"value": {
"isChecked": true,
"title": "This is a test",
"orderHint": "8585498133570680672DE"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
}
]
When I'm trying to sort by "orderHint", I get an error:
"'The template language function 'sort' did not find the named sortField 'orderHint' on one or more objects in the array."
I'm using the following expression:
sort(variables('varArrayChecked'), 'value/orderHint')
Sorting by other fields works fine, e.g.:
sort(variables('varArrayChecked'), 'id')
Is there any way how I can sort by a nested field in a JSON String?
Thanks in advance!
You can use the Advanced Data Operations connector as it will do it for you in a single step.
The Flatten Object Array step is perfect for the payload you've provided.
You can see that it will take the data, flatten it and you have the ability to sort it on the way out (noting that the Array variable contains the exact JSON you provided in your question) ...
Note: Balance Output must be set to true in order for the sorting to occur.
Result
This is the resulting JSON order by orderHint ascending.
[
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
},
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
}
]
... and to show it in descending order (which is obvious, but simply change the sort order object value from Asc to Desc) ...
[
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
},
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
}
]

Printing From JSON Without Key Name

I'm getting a JSON output from the VirusTotal API, as seen below:
{
"scan_id": "id_goes_here",
"resource": "domain_goes_here",
"url": "http://example.com/",
"response_code": 1,
"scan_date": "2021-01-19 21:28:32",
"permalink": "https://www.virustotal.com/gui/url/id_goes_here/detection/u-id_goes_here",
"verbose_msg": "Scan finished, scan information embedded in this object",
"filescan_id": null,
"positives": 0,
"total": 83,
"scans": {
"CMC Threat Intelligence": {
"detected": false,
"result": "clean site"
},
"CLEAN MX": {
"detected": false,
"result": "clean site"
},
"DNS8": {
"detected": false,
"result": "clean site"
},
"MalwareDomainList": {
"detected": false,
"result": "clean site",
"detail": "http://www.malwaredomainlist.com/mdl.php?search=example.com"
...
I want to go through this JSON and find the entries that have detected as true and if so print the name. As well, print the reference (if there is one)
For example, let's say the entry for MalwareDomainList had detected set to true. So as below:
"MalwareDomainList": {
"detected": true,
"result": "malicious site",
"detail": "http://www.malwaredomainlist.com/mdl.php?search=example.com"
I want to print the name of the list, so in this case, MalwareDomainList and also print the value of detail. As you can see in the above output, not all lists have a detail field.
In python3, I am thinking I need to have something like:
scan_content = json.dumps(response.json(), indent =4 )
for scan in scan_content["scans"]:
if scan["detected"]:
print("Name of blacklist:", scan["name"])
But getting an error since name is not an actual key in the output. Not sure how to alter my above code to achieve the following:
If detected is set to true
Print the name of the list
If applicable, print value in the detail key
Trying the below solution as such:
I have the following:
json_data = json.dumps(response.json(), indent =4 )
scan_content = json_data["scans"]
positive = []
for elem in scan_content.keys():
if scan_content[elem]["detected"] == "true":
positive.append({"Name" : elem, "Detail" : scan_content[elem]["detail"]})
print(*positive)
I get the error
scan_content = json_data["scans"]
TypeError: string indices must be integers

How to reuse json in RAML example

I have the following files
user.json
"user": {
"id": 1,
"name": "nameuser",
"online": true,
"profile": {
"photo": "",
"validated": true,
"popular": true,
"suspect": false,
"moderator": false,
"age": "22 ani",
"gender_id": "M"
}
}
profile.raml
displayName: Profile
get:
description: Get profile data
queryParameters:
userId:
description: The user id for which we are requesting the profile data
type: integer
required: true
responses:
200:
body:
application/json:
example: |
{
"user": !include user.json,
"details": {
"friend": true
}
}
The user json is present in more examples and I want to reuse it.
I'm using raml2html and it compiles it to
so how do I do this ?
I have used parameters successfully in the past. You will not be able to put a parameter inside an included file because RAML views all included files as strings. But you can do something like this in your profile.raml:
example: |
{
"user": <<userItem>>,
"details": {
"friend": true
}
}
The RAML 200 Tutorial has a good explanation and code examples (see snippets below) on how to declare parameters and them pass them in. I highly recommend reading the entire tutorial though.
resourceTypes:
- collection:
description: Collection of available <<resourcePathName>> in Jukebox.
get:
description: Get a list of <<resourcePathName>>.
responses:
200:
body:
application/json:
example: |
<<exampleCollection>>
/songs:
type:
collection:
exampleCollection: !include jukebox-include-songs.sample

How to load multidimensional / nested json into a store?

So I'm trying to load the data received from a webservice into a sencha touch 2 store.
The data is nested JSON, however it is made to include multiple dataArrays.
I am working with sencha touch 2.3.1, somewhat equal to Ext JS 4.2. I don't have that much experience with sencha yet, but I'm getting there. I decided to go for MVC, so I'd like the answers to be as close to this as possible :).
This is the example JSON I am using:
[
{
"DataCollection": {
"DataArrayOne": [
{
"Name": "John Smith",
"Age": "19"
},
{
"Name": "Bart Smith",
"Age": "16"
}
],
"DataArrayTwo": [
{
"Date": "20110601",
"Product": "Apple",
"Descr": "",
"Remark": ""
},
{
"Date": "20110601",
"Product": "Orange",
"Descr": "",
"Remark": ""
},
{
"Date": "20110601",
"Product": "Pear",
"Descr": "",
"Remark": ""
}
],
"DataArrayThree": [
{
"SomeTotalCost": "400,50",
"IntrestPercentage": "3"
}
]
}
}
]
Through only one call, I get this json. I don't want to cause any unnecessary traffic so I hope to be able to use the data somehow.
I want to be able to use each DataArray on its own.
The data gets sent to the store through its proxy:
Ext.define("MyApp.store.myDataObjects", {
extend: "Ext.data.Store",
config: {
model: "MyApp.model.myDataObject",
proxy: {
reader: {
type: "json",
rootProperty: "DataCollection"
},
type: "ajax",
api: {
read: "https://localhost/Service.svc/json"
},
limitParam: false,
startParam: false,
pageParam: false,
extraParams: {
id: "",
token: "",
filter: ""
},
writer: {
encodeRequest: true,
type: "json"
}
}
}
});
I am a bit stuck with the model here. I tried using mappings which would look like this:
config: {
fields: [ {
name: "IntrestPercentage",
mapping: "Calculation.IntrestPercentage",
type: "string"
}
]}
I tried associations as well but to no avail.
According to google chrome console, it doesn't make any objects containing data. I get only 1 object with all values "null".
My endgoal is to be able to show each dataArray in a separate table. So a table for DataArrayOne, a table for DatarrayTwo... The data itself isn't linked. They are only details that have to be shown on a view.
John Smith isn't related to the apples, as in he didn't buy. The apples are just there as an item to be shown.
The possible solutions I've seen yet not understood due to them being outdated are:
ChildStores: You have a master store that receives the data, and then
you split the data to other stores according to rootProperty. I have
no idea how to do this however and I'm not sure if it will work at
all.
Associations, in case I was doing them wrong. I don't think they
are needed because the data isn't linked to each other but it is part
of "DataCollection" though.
Could someone please post an example on how to deal with this unusual(?) kind of nested json.
Or any other solution which will lead to being able to use the 3 dataArrays at will.
Thanks in advance
The best would be to load the complete data with a separate Ext.Ajax.request and then use store.loadData in the success callback. For example:
var data = Ext.decode(response.responseText);
store1.loadData(data[0].DataCollection.DataArrayOne);
store2.loadData(data[0].DataCollection.DataArrayTwo);
store3.loadData(data[0].DataCollection.DataArrayThree);

Customizing JSON output CakePHP

$user = $this->User->find( 'all' );
$this->set( 'users', $user );
I have this code in my controller.
In my view I have this.
echo json_encode( compact( 'users' ) );
It outputs json like this
{
"users": [{
"User": {
"user_id": "2",
"email": "email#test.com",
"name": "Blah"
}]
}
}
Is there anyway to format this to remove the entire array wrapped in "users", and also remove every object being a member of "User".
This makes it harder to use on the front end. I'd like it to look like this.
[{
"user_id": "2",
"email": "email#test.com",
"name": "Blah"
}]
Thanks for any help.
I don't fully understand what you mean by "remove the entire array wrapped in "users"" and "remove every object being a member of "User"", but according to your desired output format example, you'll need to extract and pass the exact data that you want to be encoded to json_encode, instead of passing everything using compact.
Extracting could be done with the Set or the Hash class (depending on your Cake version)
Assuming your model returns the data in the default CakePHP format, this for example:
json_encode(Set::extract('/User/.', $users));
should give you a structure like this:
[{
"user_id": "2",
"email": "email#test.com",
"name": "Blah"
}]
and with multiple users it should look like this
[{
"user_id": "1",
"email": "foo#test.com",
"name": "Bar"
},
{
"user_id": "2",
"email": "email#test.com",
"name": "Blah"
}]
Use like this:
$users= (Set::extract('/User/.', $users));
pr($users);
It will remove Model from result Array and then json_encode or whatever further usage.
More library functions Set class Here and Hash class Here