简体   繁体   English

多对多关系Json

[英]Many to Many relationship Json

I am using Doctrine as an ORM. 我正在使用Doctrine作为ORM。 I'm pretty new to this. 我对此很陌生。 So there is Json response I want to send of the entities. 因此,我想发送有关实体的Json响应。 The entities are the following : 实体如下:

  • Medicine 医学
  • Generics 泛型
  • Content 内容

Medicine and Generics have a many to many relationship and Generic and Content also have a many to many relationship. 医学和仿制药具有多对多的关系,仿制药和内容也具有多对多的关系。 I need to show the details of the medicine as well as the generics of the medicine and the contents of the generics. 我需要显示药物的详细信息以及药物的仿制药和仿制药的含量。

Here is my code 这是我的代码

public function jsonSerialize()
{
    $newArray= array();
    $generics=$this->getGenerics();
    foreach ($generics as $generic){
        $array = json_decode(json_encode($generic), true);
        var_dump($array);
        array_push($newArray, $array);
    }
    var_dump($newArray);
    $xyz=array();
    $xyz1=array();
    $arratJosn = array(
            'medicineId' => $this->id,
            'medicineName' => $this->medicineName,
            'medicineQuantity' => $this->medicineQuantity,
            'medicinePrice' =>$this->medicinePrice,
            'manufactureId' =>$this->manufactureId->getmanufatureName(),
            'medicineApproved' => $this->medicineApproved,
            'medicineVersion' =>$this->medicineVersion,
            'genericDetails'=> array('genericName'=>$xyz,
                                     'genericQuantity' => $xyz1,),

    );

    foreach ($generics as $generic){
        $genericName=$generic->getGenericName();
        array_push($arratJosn['genericDetails']['genericName'], $genericName);
        $contents=$generic->getContent();
        foreach ($contents as $content){
            $genericQuantity=$content->getGenericQuantity();
            array_push($arratJosn['genericDetails']['genericQuantity'], $genericQuantity);
            $genericPrice=$content->getGenericUnit();
        }
    }

    return $this;

}

Using jsonEncode on the entity I am able to retrieve a json data , which is as follows : 在实体上使用jsonEncode,我可以检索json数据,如下所示:

{"medicineId":27,
"medicineName":"MedTest11",
"medicineQuantity":20
"medicinePrice":70,
"manufactureId":"Company2",
"medicineApproved":1,
"medicineVersion":1,
"genericDetails":{"genericName":["paracetamol","newgeneric"],
                  "genericQuantity":["30","40"]}
}

Though I want the result to be shown as 虽然我希望结果显示为

{"medicineId":27,
"medicineName":"MedTest11",
"medicineQuantity":20, 
"medicinePrice":70,
"manufactureId":"Company2", 
"medicineApproved":1, 
"medicineVersion":1,
"genericDetails":[{"genericName":"paracetamol", "genericQuantity":"30"},{"genericName":"newgeneric", "genericQuantity":"40"}] 
}

How can i achieve this? 我怎样才能做到这一点?

To achieve this you need to push an array onto genericDetails like this 为此,您需要像这样将数组推入genericDetails

$arratJosn = array(
        'medicineId' => $this->id,
        'medicineName' => $this->medicineName,
        'medicineQuantity' => $this->medicineQuantity,
        'medicinePrice' =>$this->medicinePrice,
        'manufactureId' =>$this->manufactureId->getmanufatureName(),
        'medicineApproved' => $this->medicineApproved,
        'medicineVersion' =>$this->medicineVersion,
        'genericDetails'=> array()            
);

foreach ($generics as $generic){
    $genericName=$generic->getGenericName();
    $contents=$generic->getContent();
    foreach ($contents as $content){
        $genericQuantity=$content->getGenericQuantity();
        $genericPrice=$content->getGenericUnit();
        $genericArray = array (
            'genericName' => $genericName,
            'genericQuantity' => $genericQuantity
        );            
        array_push($arratJosn['genericDetails'],$genericArray);                
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM