In yii how json formatted inputs are inserted into tables - json

I am creating project using extjs and yii. My client side design is in extjs-4 and server side design is in yii framework.
Now I am having table Poll with fields as:
pollid
pollQuestion
Isactive
Userid
And Polloption:
pollid
option
Now during creation of new pole,poll creation view form which is designed in extjs will receive inputs and will send this data to server side in json format as-
{
'success':true,
'results':[ {
'pollid' : 1,
'pollQuestion' : 'Which is capital of india',
}
{ options from polloption table in json format
}]
}
So at server side all this values will come in json format. So now in yii i want to insert this received inputs in corresponding poll tables fields.
So how Yii will convert this json formatted inputs and also insert those values into repective fields of poll table. Please help me.

Your question is very vague and general. Here's an overview of what you'll want to do:
submit the data to a Yii controller
If the data is in the body, use PHP's file_get_contents. If you POST or GET it, you can use Yii's CHttpRequest::getParam to read in the raw JSON
use CJSON::decode() to parse the JSON into a PHP array
manipulate the array values, and build a new array
return the data (echo or print it if you just need the raw JSON and don't need a view). You'll probably want to return JSON again to use it in extjs4, so you'll want to use the CJSON::encode() method

Related

Laravel generate forms with JSON

Is there a way in Laravel to save JSON code in one table field and when you load that JSON code you get generated form (view) where you can submit data that can be saved in JSON code and then again saved in one table field?
you can use Redis.
$formData = Redis::set('formdata',$json);
$formData = Redis::get('formdata');

how to convert to json from post in Yii2

I want to create json from multiple selection from listbox in yii2 after post i receive following data
categorymodel[0]=11&categorymodel[1]=12&categorymodel[2]=13
how to convert to json data and store in db.
json data like {"11","12","13"}
Once you have the proper array you can use eg:
$retJSON= json_encode($categoryModel);

Save api json to database using laravel

Is it possible to get data from an api url and save it directly to database when working with laravel? the data i get from the url is of the format {"name":"100KVA SUKAM Generator","level":"5.965"}.
Yes, you can create table with json type field (or text) and keep data there:
$table->json('data_from_api');
https://laravel.com/docs/5.2/migrations#writing-migrations
If you want to persist data as usual data, you can use mass assignment. First, convert JSON to an array with [json_decode][1] and save data like that:
$data = json_decode($jsonData, true)
Model::create($data);
Don't forget to add all columns to a $fillable property of a model.

Flowgear Insert form post data into database

How to insert post data into database using flowgear API? I am done inserting data using get but html form post method is not work using flowgear API. Please explain how it is acheive in flowgrear.
I have this URL
http://www.somedomain.com/api/flow-test
When I hit enter it is return JSON value
{
"message" : "message send successfully",
"statusCode" : "1"
}
This JSON value store into database using flowgear post method.
If you want to POST some data to a Flowgear API endpoint and then ingest that into a database, add the special property FgRequestBody on to a Variable Bar to receive the POST data.
If it's in JSON format, use JSON Convert to convert it to XML, then push it through a transform (XSL Transform or QuickMap) if it's layout doesn't match your target database.
Finally, use an SQL Table Update to insert to the data into the database.
When calling a Flowgear Workflow using an API call, the Workflow can respond with a specific body using the FgResponseBody and FgResponseContentType variables on the Variable Bar.

How to insert json encoded inputs into foreign related tables

I am creating functionalities in yii+extjs. My client side is desighned in extjs and server side is in yii framework.
I am having two tables as
Poll Option
-pollId -optionId
-PollQuestion -option
-pollId
Now through poll creation form which will be in extjs,Question and its related option gets send to server in json format. So in yii framework,in actionCreate function will get input as-
$json='{"pollId":1,"PollQuestion":"Who is the best
cricketer","option":"ABC","option":"DEF","option":"XYZ"}'
$obj=json_decode($json);
During creation of poll,user can enter any number of options.So number of options can be anything.
i am creating above functionality in Pollcontroller.
So this newly created question gets inserted into Poll table as=
$model=new Poll();
$model->pollId=$obj->pollId;
$model->PollQuestion=$obj->PollQuestion;
Now i want to put all these new options in option table with same pollId. So how to add all these options in option table? Please help me
I would start by modifying the JSON so the options are a separate JSON within the pollquestion JSON.
Something like this...
$json='{"pollId": 1,"PollQuestion": "Who is the best cricketer",
"options":{[{"value":"ABC"},{"name": "DEF"},{"name": "XYZ"}]}';
That way when you decode it with json_decode you'll get an options array which you can go through and add each of the elements in that array in the options array.
for($i=0; $i<sizeof($obj['options']);$i++){
//Add to table logic here
}