通过oneToMany和ManyToOne关系链接。我在实体Commmande上使用规范化和非规范化,因为它适用于所有领域,所以设置正确。但是在PrestationsLaboratoire上,由外键使用的现场命令始终为空。 我已经尝试阅读很多文章,但是没有结果。
/**
* @ORM\Entity
* @ApiResource(attributes={
* "normalization_context"={"groups"={"readCO"}},
* "denormalization_context"={"groups"={"writeCO"}}
* })
*
* @ApiFilter(SearchFilter::class, strategy="exact")
*
* @var Commande clients
*/
class Commande {
// les types de commandes
const ROUTINE = "ROUTINE";
const LABORATOIRE = "LABORATOIRE";
const FOURNITURE = "FOURNITURE";
// les status
const IS_ORDERED = "IS_ORDERED";
const IS_VALIDATE = "IS_VALIDATE";
const IN_PROGRESS = "IN_PROGRESS";
const IS_TERMINATED = "IS_TERMINATED";
const IS_CANCELED = "IS_CANCELED";
const ERROR_STATUS = "ERROR_SAISIE_STATUS";
/**
* @var id
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"readCO","writeCO"})
*/
protected $id;
/**
* @var le commanditaire de la commande
* @ORM\Column(type="text", length=45)
* @Groups({"readCO","writeCO"})
*/
protected $commanditaire;
/**
* @var date de la commande
* @ORM\Column(type="datetime")
* @Assert\DateTime(format="Y-m-d\TH:i:sP")
* @Groups({"readCO","writeCO"})
*/
protected $date;
/**
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\PrestationsLaboratoire",mappedBy="commande",cascade={"persist"},fetch="EAGER")
* @Groups({"readCO","writeCO"})
* @MaxDepth(2)
*
*/
protected $prestalabos;
/**
* @var les forunitures
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Fourniture")
* @Groups({"readCO","writeCO"})
* @MaxDepth(2)
*/
protected $fournitures;
/**
* @var les routines
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Routine", inversedBy="commande", cascade={"persist"},fetch="EAGER")
* @Groups({"readCO","writeCO"})
* @MaxDepth(2)
*/
protected $routine;
/**
* @var status de la commande : IS_ORDERED,IS_VALIDATED,IN_PROGRESS,IS_TERMINATED,IS_CANCELED
* @ORM\Column(type="text", length=15)
* @Groups({"readCO","writeCO"})
*/
protected $status;
/**
* @var ref de la comande pour le client
* @ORM\Column(type="text", length=30)
* @Groups({"readCO","writeCO"})
*/
protected $refClient;
/**
*
* @ORM\ManyToOne(targetEntity="Entreprise",inversedBy="commandes",fetch="EAGER",cascade={"persist"})
* @Groups({"readCO","writeCO"})
*/
protected $entreprise;
public function __construct() {
$this->prestalabos = new ArrayCollection();
$this->fournitures = new ArrayCollection();
}
public function addPrestalabos($presta)
{
$presta->setCommande($this);
$this->prestalabos->add($presta);
}
public function removePrestalabos($presta)
{
$presta->setCommande(null);
$this->prestalabos->removeElement($presta);
}
public function getPrestalabos(){
return $this->prestalabos;
}
public function setPrestalabos($prestationsLaboratoires) {
$this->prestalabos = $prestationsLaboratoires;
}
在“ $ commande”字段上与PrestationsLaboratoire实体的“一对多”字段上的
/**
* @ORM\Entity
* @ApiResource()
* @ApiFilter(SearchFilter::class, strategy="exact")
*/
class PrestationsLaboratoire {
/**
* @var id
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"readCO","writeCO"})
*/
public $id;
/**
*@var designation du test : Dosage/Test de stérilité/test endotoxine .....
*@ORM\Column(type="text",nullable=true,length=50)
*@Groups({"readCO","writeCO"})
*/
protected $designation;
/**
* @var origine du dosage :["OBLIGATOIRE","DP","SUPPLEMENTAIRES","LIBRE"]
* - DP(deviation paramétrique)
* - OBLIGATOIRE définit dans le rapport de sté
* - TRAITEMENT définit dans le rapport de traitement
* - SUPPLEMENTAIRES demande complémentaire du client
* - LIBRE commande client non liée steriservice
* @ORM\Column(type="text",length=15)
* @Groups({"readCO","writeCO"})
*
*/
protected $origine;
/**
* @var méthode uniquement pour le Dosage : Simulation ou Extraction
* @ORM\Column(type="text",nullable=true,length=45)
* @Groups({"readCO","writeCO"})
*/
protected $methode;
/**
*@var ref du rapport de la methode
*@ORM\Column(type="text",nullable=true,length=45)
*@Groups({"readCO","writeCO"})
*/
protected $rapportValidationMethode;
/**
* @var ref to sterilisation
* ORM\ManyToOne(targetEntity="Sterilisation",inversedBy="dosages",fetch="EAGER",cascade={"persist"})
* Groups({"readCO","writeCO"})
*/
protected $sterilisation ;
/**
* @var ref to traitement
* ORM\ManyToOne(targetEntity="Traitement",inversedBy="dosages",fetch="EAGER",cascade={"persist"})
*Groups({"readCO","writeCO"})
*/
protected $traitement ;
/**
* @var ref dosages obligatoires rapportSterilisation
* @ORM\ManyToOne(targetEntity="RapportSterilisation",inversedBy="dosages",fetch="EAGER",cascade={"persist"})
*@Groups({"readCO","writeCO"})
*/
protected $rapportSterilisation;
/**
* ref dosages obligatoires rapportTraitement
* @ORM\ManyToOne(targetEntity="RapportTraitement",inversedBy="dosages",fetch="EAGER",cascade={"persist"})
* @Groups({"readCO","writeCO"})
*/
protected $rapportTraitement;
/**
*@var reference du produit
*@ORM\Column(type="text",nullable=true,length=50)
*@Groups({"readCO","writeCO"})
*/
protected $ref;
/**
*@var num des lots concernés
*@ORM\Column(type="text",nullable=true,length=200)
*@Groups({"readCO","writeCO"})
*/
protected $numLots;
/**
*@var commantaires sur les tests
*@ORM\Column(type="text",nullable=true,length=200)
*@Groups({"readCO","writeCO"})
*
*/
protected $commentaire;
/**
* la commande reliée
* @ORM\ManyToOne(targetEntity="AppBundle\Entity \Commande",inversedBy="prestalabos",fetch="EAGER",cascade={"persist"})
* @Groups({"readCO","writeCO"})
*
*/
protected $commande;
我在PrestationsLaboratoire表上的外键(commande_id)始终为空,我不明白为什么?几个小时以来,我一直陷于困境。邮递员的结果是:
{
"@context": "/app_dev.php/api/contexts/Commande",
"@id": "/app_dev.php/api/commandes/55",
"@type": "Commande",
"id": 55,
"commanditaire": "bruno.rotrou@free.fr",
"date": "2018-10-15T20:23:45+02:00",
"prestalabos": [
{
"@id": "/app_dev.php/api/prestations_laboratoires/16",
"@type": "PrestationsLaboratoire",
"id": 16,
"designation": "test bruno",
"origine": "supplmentaire",
"methode": null,
"rapportValidationMethode": "ref-4588",
"rapportSterilisation": null,
"rapportTraitement": null,
"ref": null,
"numLots": "125,455,478",
"commentaire": "les tes sont dans l'aquarium",
"commande": null
}
],
"fournitures": [],
"routine": null,
"status": "IS_ORDERED",
"refClient": "ma commande",
"entreprise": "/app_dev.php/api/entreprises/2334"
}
谢谢
答案 0 :(得分:0)
我发现了错误。 对于doctrine arrayCollection,add方法必须像这样在方法末尾没有“ s”。
public function addPrestalabo($presta)
不好是
public function addPrestalabos($presta)
请注意,“ s”为减号。 我不知道symfony如何处理类似addMyProperty的方法?我曾尝试搜索解释,但原因仍未解决。