我在laravel应用程序中有一种方法:
private function mergeSurveysWithCases($surveys){
foreach($surveys as $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;
}
我们可以看到此方法的参数是$调查。这是一个JSON响应,并带有API的一系列调查。每个调查都有以下形式:
{
"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-550rn",
"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
}
}
}
在应用程序中,我有一个关系表,将对象连接的wchich称为 case 用对象 surveys 。我从此表中显示下面的实体:
<?php
namespace AppHttpEntitiesCic;
use DoctrineORMMapping as ORM;
/**
*
* @ORMEntity
* @ORMTable(name="cic_case_survey")
*/
class CicCaseSurvey
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
protected $id;
/**
*
* @ORMOneToOne(targetEntity="CicCase", cascade={"all"})
* @ORMJoinColumn(name="case_id", referencedColumnName="id")
*
*/
protected $case;
/**
*
* @ORMColumn(name="survey_id", type = "integer")
*
*/
protected $survey;
/**
* @return mixed
*/
/**
* @ORMColumn(type="datetime")
* @var datetime
*/
protected $created_at;
/**
* @ORMColumn(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;
}
}
API响应后,我试图根据表CIC_CASE_SURVEY
连接来自调查中的数据的案例的数据。正如我们可以从方法 MergesUrveySwithCases 中看到的那样,我已经在循环中合并了 findOneby 方法,用于每个连接情况数据(案例ID和FirstContact)与调查数据。我认为,这种解决方案不是最佳的。我认为对于大量数据来说,它会非常有效。有人知道合并这些对象的更好解决方案吗?我会非常感谢最好的问候;)
因此,在您的情况下,应将任务删除为"从json数组中的实体"。除了,也许您应该在一些第三方序列化库库的帮助下完成,这些库将为您完成大部分工作。我个人使用jmsserializer:这取决于学说注释 - 因此,您只需要在您的实体上添加注释元数据,并向Serialialialser提供众议院化的实体类型即可获得此工作。您可以在此处获取更多详细信息:https://jmsyst.com/libs/serializer