如何使用Yii从数据库中的JSON帖子中保存多个项目
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Yii从数据库中的JSON帖子中保存多个项目相关的知识,希望对你有一定的参考价值。
我在这里想要实现的是,只要有新的注册,就可以将JSON后期数据中的多个项目保存到数据库中。
我已经成功保存了一次注册。但是,如果我有多个注册,它将不会保存所有传递的数据。它只会保存第一个项目。
这是捕获的JSON帖子数据:
{"id" : "1514358676612-F9RM4",
"accountId" : 128768,
"eventId" : 206218,
"timestamp" : 1514358676612,
"url" : "http://hub.mymagic.my/bizzabo/bizzaboCallback",
"details" : {
"items" : [ {
"contactEmail" : "ikram@mymagic.my",
"created" : "2017-12-27T07:09:21.000+0000",
"ticketPaid" : 0,
"contactFirstName" : "Ikram",
"contactLastName" : "Khasim",
"currency" : "USD",
"validity" : "valid",
"status" : "completed",
"ticketType" : "test",
"ticketId" : 1296403,
"invoice" : false,
"contactName" : "Ikram Khasim",
"paymentStatus" : "completed",
"registrationForm" : {
"type_of_person" : "Experienced Entrepreneur",
"firstName" : "Ikram",
"organisation__instit" : "MaGIC",
"lastName" : "Khasim",
"gender" : "Male | Lelaki",
"age_group" : "22-24",
"mobileNumber" : "0193533005",
"email" : "ikram@mymagic.my",
"how_did_you_hear_abo" : "Word of mouth"
},
"type" : "ticket"
}, {
"contactEmail" : "yasmin@mymagic.my",
"created" : "2017-12-27T07:09:21.000+0000",
"ticketPaid" : 0,
"contactFirstName" : "Ikram",
"contactLastName" : "Khasim",
"currency" : "USD",
"validity" : "valid",
"status" : "completed",
"ticketType" : "test",
"ticketId" : 1296404,
"invoice" : false,
"contactName" : "Ikram Khasim",
"paymentStatus" : "completed",
"registrationForm" : {
"type_of_person" : "Experienced Entrepreneur",
"firstName" : "Ikram",
"organisation__instit" : "MaGIC",
"lastName" : "Khasim",
"gender" : "Male | Lelaki",
"age_group" : "22-24",
"mobileNumber" : "0193533005",
"email" : "yasmin@mymagic.my",
"how_did_you_hear_abo" : "Word of mouth"
},
"type" : "ticket"
} ],
"contactEmail" : "ikram@mymagic.my",
"contactFirstName" : "Ikram",
"contactLastName" : "Khasim",
"currency" : "USD",
"action" : "charge",
"status" : "completed",
"quantity" : 2,
"invoice" : false,
"contactName" : "Ikram Khasim",
"paymentStatus" : "completed",
"amount" : 0,
"orderId" : 651560
},
"type" : "orderCreated"
}
这是将JSON后期数据项保存到数据库中的代码。它成功地将第一个项目存储到数据库中,但是没有保存第二个项目。
public function actionBizzaboCallback()
{
$junk = new Junk;
$junk->code = 'bizzabo-bizzaboCallback-'.time();
$junk->content .= serialize($_POST);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
$junk = new Junk;
$junk->code = 'bizzabo-getJSONError-'.time();
$junk->content = sprintf('JSON not captured at : %s', Yii::app()->params['masterDomain']);
die(header('HTTP/1.0 415 Unsupported Media Type'));
} else {
// check if registration exist
// decode json
$objects = json_decode($json);
//create new registration
$object = new EventRegistration;
$object->event_code = $objects->eventId;
$tmps = HUB::getEventCode($object->event_code);
$object->event_id = $tmps->id;
$object->event_vendor_code = 'bizzabo';
$object->registration_code = $objects->details->items[0]->ticketId;
$object->full_name = sprintf('%s %s', $objects->details->items[0]->registrationForm->firstName, $objects->details->items[0]->registrationForm->lastName);
$object->first_name = $objects->details->items[0]->registrationForm->firstName;
$object->last_name = $objects->details->items[0]->registrationForm->lastName;
$object->email = $objects->details->items[0]->registrationForm->email;
$object->date_registered = time();
$object->date_payment = time();
$object->json_original = json_decode($json,true);
$object->date_added = time();
$object->date_modified = time();
if($object->save()){
$success = 'successfully insert data into db';
}else{
$fail = $object->errors;
$status = 'failed insert data into db';
}
//create junk
$junk = new Junk;
$junk->code = 'bizzabo-getBizzaboRegistrationIntoDB-'.time();
$junk->content = sprintf('Success(%s) or Fail(%s) with error message (%s). With first name is %s and last name is %s. JSON Captured: %s', $success, $status, $fail, $object->first_name, $object->last_name, $json);
}
}
$junk->save();
}
答案
您应该遍历项目内容以保存相关值,例如:
} else {
// check if registration exist
// decode json
$objects = json_decode($json);
foreach( $object->details->items as $key = $value){
//create new registration
$object = new EventRegistration;
$object->event_code = $objects->eventId;
$tmps = HUB::getEventCode($object->event_code);
$object->event_id = $tmps->id;
$object->event_vendor_code = 'bizzabo';
// apply the actual item value
$object->registration_code = $value->ticketId;
$object->full_name = sprintf('%s %s',
$value->registrationForm->firstName,
$value->registrationForm->lastName);
.......
......
}
以上是关于如何使用Yii从数据库中的JSON帖子中保存多个项目的主要内容,如果未能解决你的问题,请参考以下文章