Yii2 how to save received JSON data into database

风流意气都作罢 提交于 2020-01-06 06:48:04

问题


I have created an API which returns me an array of data in json

Array ( [0] => stdClass Object ( [MSN] => 002999001207 [PingDateTime] => 2018-05-04T16:33:27 [PingValue] => 22 ) [1] => stdClass Object ( [MSN] => 002999001195 [PingDateTime] => 2018-05-04T16:34:11 [PingValue] => 21 ) [2] => stdClass Object ( [MSN] => 002999001180 [PingDateTime] => 2018-05-04T14:42:40 [PingValue] => 20 ) [3] => stdClass Object ( [MSN] => 002999001157 [PingDateTime] => 2018-05-04T14:42:52 [PingValue] => 30 ) [4] => stdClass Object ( [MSN] => 002999001142 [PingDateTime] => 2018-05-04T16:37:19 [PingValue] => 13 ) [5] => stdClass Object ( [MSN] => 002999001138 [PingDateTime] => 2018-05-04T16:32:22 [PingValue] => 20 ) [6] => stdClass Object ( [MSN] => 002999001114 [PingDateTime] => 2018-05-04T16:32:52 [PingValue] => 22 )

Now, I am trying to save it in my DB

    $curl = curl_init($api_url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Key'));

    $m->start_date_time = date('Y-m-d h:i:s');

    $curl_response = curl_exec($curl);
    $json = json_decode($curl_response);
    $record = $json->data;

    foreach ($record as $item){
        if($this->isSaved($item->MSN))
        {
            return false;
        }
        else if($this->ogpCreated($item->MSN))
        {
           $m->end_date_time = date('Y-m-d h:i:s');
           $m->meter_msn = $item->MSN;
           $m->meter_id = Meters::msnmapToid($m->meter_msn);
           $m->sub_div_code = Ogpdetail::msnTosubdiv($item->MSN);
           $m->sub_div_name = Ogpdetail::subDivToName($m->sub_div_code);
           $m->meter_ping_date_time = str_replace('T', ' ', $item->PingDateTime);
            $m->save();
        }
    }
    return $this->redirect(['index']);

In above code, there are two if conditions

isSaved($item->MSN)

 $meter = MeterPing::find()->where(['meter_msn' => $msn])->one();

    if($meter)
        return true;
    return false;

From the above function, I am trying to check whether the incoming MSN is already saved or not. If it's already present in the table it will not save that particular MSN but yes save all the other MSN that are not saved previously.

ogpCreated($item->MSN)

 $meter = Ogpdetail::find()->where(['meter_serial' => $msn])->one();

    if($meter)
        return true;
    return false;

From the above function, I am trying to check that the incoming MSN is OGP created or not. Again it should not save any MSN which is not OGP created.

Now, when I try to run this Create function it only saves one record at a time.

I think there is some issue in if..... elseif that only allows saving one entry. But I am not sure of that.

Update 1

I have tried to remove the checks and then save the incoming data but still, it only saves one record

How can I save the entire received JSON data into my DB with all checks working?


回答1:


You need to add your model initialization $m=new YourModel(); inside the foreach loop an within the elseif($this->ogpCreated($item->MSN)), that is why it is saving only one record

foreach ($record as $item){

 if($this->isSaved($item->MSN)){
        return false;
 }
 else if($this->ogpCreated($item->MSN)){
    $m= new YourModel();

You are using the $m in the curl command too it would be better to post all code incase you run into some logical errors you need to adjust the code accordingly, or change the name of the variable from $m inside the foreach

Apart from this you can use this extension Yii2-Curl for curl commands it will allow you more flexibility to your code in a more readable way



来源:https://stackoverflow.com/questions/50176679/yii2-how-to-save-received-json-data-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!