0
votes

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

1

1 Answers

0
votes

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
}