Yii2如何将接收到的JSON数据保存到数据库中 [英] Yii2 how to save received JSON data into database
问题描述
我创建了一个 API
,该API返回了 json
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 )
现在,我正在尝试将其保存到我的 DB
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']);
在上面的代码中,有两个if条件
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;
从上述功能中,我试图检查传入的 MSN
是否已保存。如果它已经存在于表中,则不会保存该特定的 MSN
,但是会保存所有其他的 MSN
以前保存。
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;
从上面的函数中,我试图检查传入的 MSN
是否已创建 OGP
。同样,它不应保存任何未创建的 OGP
的 MSN
。
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.
现在,当我尝试运行此 Create
函数时,一次只保存一个记录。
Now, when I try to run this Create
function it only saves one record at a time.
我认为 if ... elseif
中存在一些问题,该问题仅允许保存一个条目。但是我不确定。
I think there is some issue in if..... elseif
that only allows saving one entry. But I am not sure of that.
更新1
我尝试删除支票,然后保存传入数据,但仍然只保存一条记录
I have tried to remove the checks and then save the incoming data but still, it only saves one record
如何将接收到的全部 JSON
数据保存到我的 DB
可以执行所有检查?
How can I save the entire received JSON
data into my DB
with all checks working?
推荐答案
您需要添加模型初始化 $ m = new YourModel(); $ c在
foreach
循环内的$ c>在 elseif($ this-> ogpCreated($ item-> MSN))$ c中$ c>,这就是为什么它仅保存一条记录的原因
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();
您正在使用 $ m
curl命令也最好发布所有代码,以防万一您遇到一些逻辑错误,需要相应地调整代码,或者从内部的 $ m
更改变量的名称 foreach
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
除此之外,您可以使用此扩展名 Yii2-Curl
用于curl命令,它将为您提供更大的灵活性
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
这篇关于Yii2如何将接收到的JSON数据保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!