PHP reports based on Json template - php

I am developing a system of forms built on Json data, in the form of form defitions, outputs, and then report templating. My app is based on Symfony2 and uses AWS S3 for the Json storage, but I should be able to move any generic PHP to that framework.
Basically, I need to be able to update the template json node which will then change the way that records are displayed in a page. The tough thing is that the output template will be multi level. Ie we may want to group on Name and then on Date at the next level down. The following is an example of how I might want it all to look.
QUESTION JSON - a definition of all questions
"questions": [
{
"id": 0,
"question": {
"question": "Name",
"type": "Textbox",
}
},
{
"id": 1,
"question": {
"question": "Email",
"type": "Textbox",
}
},
{
"id": 2,
"question": {
"question": "Date",
"type": "DateTime",
}
}
]
ANSWER ARRAY JSON - one for each form filled out
"Questions" : [
{
"answer" : "john smith"
"question" : 0
},
{
"answer" : "js#abc.com"
"question" : 1
},
{
"answer" : "01/01/2015"
"question" : 2
}
]
"Questions" : [
{
"answer" : "john smith"
"question" : 0
},
{
"answer" : "js_secondemail#abc.com"
"question" : 1
},
{
"answer" : "01/01/2015"
"question" : 2
}
]
OUTPUT TEMPLATE JSON - a definition of how we want the data to be displayed
"outputStructure": [{
"output": [{
"showReport": true,
"groupBy": 'Name'
}]
}]
And that should all end up with my php page displaying the following:
John Smith (link)
===js#abc.com.au
===js_secondemail#abc.com.au
If anyone can help it will be much appreciated!

Related

Exempt a specific attribute from been selected in Laravel Mysql Json Column

I am trying to return results of a specific table in Laravel while hiding a specific attribute from the returned SQL results.
Part of the JSON response is as shown
...
"custom_filters": [
{
"question_type": "single",
"question": "How long have you been an influencer",
"answer": 1
},
{
"question_type": "choice",
"question": "Have you ever went viral?",
"choices": [
"yes",
"no"
],
"answer": "yes"
},
{
"question_type": "single",
"question": "How many followers do you have on Instagram",
"answer": 12000
}
],
...
Expected Output
...
"custom_filters": [
{
"question_type": "single",
"question": "How long have you been an influencer",
},
{
"question_type": "choice",
"question": "Have you ever went viral?",
"choices": [
"yes",
"no"
]
},
{
"question_type": "single",
"question": "How many followers do you have on Instagram"
}
],
...
I want the answer part of the JSON OBJECT to be hidden/removed in the final output as shown above. I know i could get the results from my Eloquent model and filter the results accordingly discarding the answer part. but I feel like there is a better way of doing this.
by the way here is the code for fetching the results
$posts = Posts::where('status', 'published')->orderBy('post_id', 'desc')
->paginate(30);
use this
Posts::where('status', 'published')->orderBy('post_id', 'desc')->whereJsonContains('json_data', [['custom_filters' => ['answer' => 'yes']]])->get()

Any way to prevent Doctrine result to nest entities when adding custom field to QueryBuilder?

I am adding a calculated field named 'distance' to a Doctrine query but the result is then nesting the entities as follow:
[
{
"0": {
"name": "Some name",
"id": 3
},
"distance": "10"
},
{
...
]
Is there a way to tell Doctrine to format the response like this instead?
[
{
"name": "Some name",
"id": 3
"distance": "10"
},
{
...
]
I don't always add this field as it depends of the search criteria, so I am having inconsistent result format.
Also I can prevent the issue by adding the distance field as HIDDEN, but then I lose the distance information, which I would like to keep.
Any help appreciated, thanks.

I want a more consolidate json format

I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding

Elasticsearch: What's the best way to search for a word within a string AND get score?

I'm using ElasticSearch's PHP client and I find really difficult to return results with scores whenever I want to search for a word that is "hidden" within a string.
This is an example:
I want to get all the documents where the field "file" has the word "anses" and files are named like this:
axx14anses19122015.zip
What I know about it
I know I should tokenize those words, can't realize how to do it.
Also I've read about aggregations but I'm really new to ES and I have to deliver a working piece ASAP.
What I've tried so far
REGEXP: using regular expressions is very expensive and does not return any scores, which is a must-to-have in order to shrink results and bring the user accurate information.
Wildcards: same thing, slow and no scores
Own script where I have a dictionary and search for critical words using regexp, if match, create a new field within that matched document with the word. The reason is to create a TOKEN so in future searches I can use regular match with scores. Negative side: the dictionary thing was totally denied by my boss so I'm here asking for any ideas.
Thanks in advance.
I suggest in your case nGram tokenizer see the example
I will create a analyzer and a mapping for a doc type
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"tokenizer": {
"ngram_tokenizer": {
"type": "nGram",
"min_gram": 4,
"max_gram": 4,
"token_chars": [ "letter", "digit" ]
}
},
"analyzer": {
"ngram_tokenizer_analyzer": {
"type": "custom",
"tokenizer": "ngram_tokenizer",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"text_field": {
"type": "string",
"term_vector": "yes",
"analyzer": "ngram_tokenizer_analyzer"
}
}
}
}
}
after that I`ll insert a document using your file name
PUT /test_index/doc/1
{
"text_field": "axx14anses19122015"
}
now I`ll just will use a query match
POST /test_index/_search
{
"query": {
"match": {
"text_field": "anses"
}
}
}
and will receive a reponse like this
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.10848885,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.10848885,
"_source": {
"text_field": "axx14anses19122015"
}
}
]
}
}
What i did?
i just created a nGram tokenizer that will explode our string in 4 characters terms and will index this terms separated and they will be searched when I search a part of the string.
To see more, read this article https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch
Hope it help!
Ok after trying -so- many times it worked. I'll share the solution just in case someone else needs it. Thank you so much to Waldemar, it was a really good approach and I still cannot see why it's not working.
curl -XPUT 'http://ipaddresshere/tokentest' -d
'{ "settings":
{ "number_of_shards": 1, "analysis" :
{ "analyzer" : { "myngram" : { "tokenizer" : "mytokenizer" } },
"tokenizer" : { "mytokenizer" : {
"type" : "nGram",
"min_gram" : "3",
"max_gram" : "5",
"token_chars" : [ "letter", "digit" ] } } } },
"mappings":
{ "doc" :
{ "properties" :
{ "field" : {
"type" : "string",
"term_vector" : "yes",
"analyzer" : "myngram" } } } } }'
Sorry for bad indentation, I'm really hurry but want to post the solution.
So, this will take any string from "field" and split it into nGrams with lenght 3 to 5. For example: "abcanses14f.zip" will result in:
abc, abca, abcan, bca, bcan, bcans, etc... until it reaches anses or a similar term which is matcheable and has a score related to it.

sql database design: model attribute has infinite results

I'm currently storing user-generated surveys in JSON files, and am now converting these to a sql database. Regarding this portion of JSON:
"surveys": [
{
"surveyId": 1,
"name": "Landing Page Survey",
"active": true,
"panes": [
{
"type": "question",
"name": "Question",
"head": "Is there anything preventing you from signing up for a free 14-day trial?",
"response": "textbox",
"options": [
{
"data": "Time",
"target": "Response 1",
"placeholder": "",
"list": "f7b3cdeed8"
},
{
"data": "Money",
"target": "Response 1",
"placeholder": "",
"list": "local"
},
{
"data": "I'm not interested",
"target": "Thanks",
"placeholder": "",
"list": "local"
}
],
"button": "Send"
},
{
"type": "response",
"name": "Response 1",
"head": "Thanks for your interest in our product. Enter your email address to have a team member follow-up with you.",
"response": "email",
"options": [
{
"data": "data",
"target": "Thanks",
"placeholder": "Your email",
"list": "f7b3cdeed8"
}
],
"button": "Submit"
},
{
"type": "thanks",
"name": "Thanks",
"head": "Thanks for your feedback!",
"response": "multichoice",
"options": [
{
"data": "data",
"target": "",
"placeholder": "put response here",
"list": "local"
}
],
"button": "Button text"
}
]
The JSON isn't that important, just posting so you can see the idea.. surveys.panes are the questions of the survey. Each survey could have an infinite number of questions in it, so I'm struggling with how to store those.
Originally I was thinking of having a surveys table with columns for the questions, 'question_1_type, question_1_text', etc.. This would work if each survey was limited to an amount of questions (10 for example, so I could create 10 sets of columns). This feels horribly wrong though.
Or is it more correct to create a questions table, and for each question in a survey create a row in the questions table. Link it to the survey table with an id, then when you want to output the survey JSON for an API or whatever, just do a bunch of joins across the survey and questions table.
Also, if a survey question is multiple choice, it could have an infinite number of response options also, so would you have to build a table for those as well? Then for each survey JSON build you'd have to join across the surveys, questions, and question_options table. That seems like a lot of overhead running all the joins..
But as I understand, it's incorrect to store anything but one value in a column (an array for example) as it defeats the relational idea of a sql db.
Very noob question.. I haven't quite wrapped my head around correct database design. Appreciate any help!

Categories