laravel - Merge received data from API with data from database -
i have method in laravel application:
private function mergesurveyswithcases($surveys){ foreach($surveys $survey ){ $surveyid = $survey->survey->id; $casesurvey = $this->surveysrepository->findoneby(['survey' => $surveyid]); $caseid = $casesurvey->getcase()->getid(); $firstcontant = $this->caserepository->getfirstcontactbycase($caseid); $survey->casedata = ["caseid" => $caseid, "firstcontact" => $firstcontant ]; } return $surveys; }
as can see argument of method $surveys. json response array of surveys api. every survey has form below:
{ "hash": "8537f99bb4166da9f74f02ebb70907cf", "sending_date": "2017-08-23t08:32:37+02:00", "expiration_date": "2018-02-23t08:32:37+01:00", "created_at": "2017-08-23t08:32:37+02:00", "trans_id": "10-550\r\n", "id": 18, "status": { "name": "new", "id": 1 }, "base_survey": { "name": "base survey test 1", "created_at": "2017-07-25t10:08:18+02:00", "creator_trans_id": "10-615", "date_start": "2017-07-25t00:00:00+02:00", "expiration_int": 6, "expiration_string": "months", "survey_prefix": "psi", "welcome_message": "test1", "expiration_message": "test2", "id": 1, "template": { "path": "/template_ssi_1/", "name": "ssi", "id": 1 } } }
in application have relational table connect objects wchich called cases object surveys. show below entities table:
<?php namespace app\http\entities\cic; use doctrine\orm\mapping orm; /** * * @orm\entity * @orm\table(name="cic_case_survey") */ class ciccasesurvey { /** * @orm\id * @orm\generatedvalue * @orm\column(type="integer") */ protected $id; /** * * @orm\onetoone(targetentity="ciccase", cascade={"all"}) * @orm\joincolumn(name="case_id", referencedcolumnname="id") * */ protected $case; /** * * @orm\column(name="survey_id", type = "integer") * */ protected $survey; /** * @return mixed */ /** * @orm\column(type="datetime") * @var datetime */ protected $created_at; /** * @orm\column(type="datetime") * @var datetime */ protected $updated_at; public function getcreatedat() { return $this->created_at; } public function setcreatedat() { $this->created_at = carbon::now(); } public function getupdatedat() { return $this->updated_at; } public function setupdatedat() { $this->updated_at = carbon::now(); } public function getsurvey() { return $this->survey; } /** * @param mixed $survey */ public function setsurvey($survey) { $this->survey = $survey; } public function getcase() { return $this->case; } /** * @param mixed $case */ public function setcase($case) { $this->case = $case; } /** * @return mixed */ public function getid() { return $this->id; } /** * @param mixed $id */ public function setid($id) { $this->id = $id; } }
after response api i'm trying connect based on table cic_case_survey
data cases data in surveys obejcts. can see method mergesurveyswithcases i've done merging in loop use findoneby method every connection case data(case id , firstcontact) survey data. in opinion solution not optimal. think work bad lot of data. perhaps know better solution of merging objects? grateful best regards ;)
so, in case task should decribed "deserialize entity json array". than, maybe should make of third-party serializer library of work you. use jmsserializer: depends on doctrine annotations - need add annotations metadata on entities , provide deserialized entity type serializer work. can more details here: https://jmsyst.com/libs/serializer
Comments
Post a Comment