你好,我面临这个异常:
App\Entity\CrfCvs类型的实体缺少字段"cvsIde"的指定ID。此实体的标识符生成策略要求在调用EntityManager#persist((之前填充ID字段。如果您想要自动生成标识符,则需要相应地调整元数据映射。
我有一个对象CrfCvs,它有两个相关的对象:CvsVts和CvsUtl,就像这样:CrfCvs
<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* CrfCvs
*
* @ORMTable(name="CRF_CVS")
* @ORMEntity
*/
class CrfCvs
{
/**
* @var float|null
*
* @ORMColumn(name="CVS_ECG", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsEcg;
/**
* @var float|null
*
* @ORMColumn(name="CVS_BLO_DRA", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsBloDra;
/**
* @var string|null
*
* @ORMColumn(name="CVS_END_ISS", type="string", length=255, nullable=true)
*/
private $cvsEndIss;
/**
* @var float|null
*
* @ORMColumn(name="CVS_END_RHI", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsEndRhi;
/**
* @var DateTime|null
*
* @ORMColumn(name="CVS_END_DAT", type="datetime", nullable=true)
*/
private $cvsEndDat;
/**
* @var float|null
*
* @ORMColumn(name="CVS_HEA_HCT", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsHeaHct;
/**
* @var float|null
*
* @ORMColumn(name="CVS_HEA_HB", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsHeaHb;
/**
* @var float|null
*
* @ORMColumn(name="CVS_NUM_DAY", type="float", precision=10, scale=0, nullable=true)
*/
private $cvsNumDay;
/**
* @var DateTime|null
*
* @ORMColumn(name="CVS_DAY", type="datetime", nullable=true)
*/
private $cvsDay;
/**
* @var string|null
*
* @ORMColumn(name="CVS_LAS_ID", type="string", length=255, nullable=true)
*/
private $cvsLasId;
/**
* @var string|null
*
* @ORMColumn(name="CVS_OXF_ID", type="string", length=255, nullable=true)
*/
private $cvsOxfId;
/**
* @ORMColumn(name="CVS_IDE", type="string", length=255, nullable=false)
* @ORMId
*/
private $cvsIde;
/**
* @ORMOneToMany(targetEntity="AppEntityCvsVts", mappedBy="cvsIde", cascade={"persist"})
*/
private $cvsVts;
/**
* @ORMOneToMany(targetEntity="AppEntityCvsUlt", mappedBy="cvsIde", cascade={"persist"})
*/
private $cvsUlt;
public function __construct()
{
$this->cvsUlt = new ArrayCollection();
$this->cvsVts = new ArrayCollection();
}
/**
* @return float|null
*/
public function getCvsEcg(): ?float
{
return $this->cvsEcg;
}
/**
* @param float|null $cvsEcg
*/
public function setCvsEcg(?float $cvsEcg): void
{
$this->cvsEcg = $cvsEcg;
}
/**
* @return float|null
*/
public function getCvsBloDra(): ?float
{
return $this->cvsBloDra;
}
/**
* @param float|null $cvsBloDra
*/
public function setCvsBloDra(?float $cvsBloDra): void
{
$this->cvsBloDra = $cvsBloDra;
}
/**
* @return string|null
*/
public function getCvsEndIss(): ?string
{
return $this->cvsEndIss;
}
/**
* @param string|null $cvsEndIss
*/
public function setCvsEndIss(?string $cvsEndIss): void
{
$this->cvsEndIss = $cvsEndIss;
}
/**
* @return float|null
*/
public function getCvsEndRhi(): ?float
{
return $this->cvsEndRhi;
}
/**
* @param float|null $cvsEndRhi
*/
public function setCvsEndRhi(?float $cvsEndRhi): void
{
$this->cvsEndRhi = $cvsEndRhi;
}
/**
* @return DateTime|null
*/
public function getCvsEndDat(): ?DateTime
{
return $this->cvsEndDat;
}
/**
* @param DateTime|null $cvsEndDat
*/
public function setCvsEndDat(?DateTime $cvsEndDat): void
{
$this->cvsEndDat = $cvsEndDat;
}
/**
* @return float|null
*/
public function getCvsHeaHct(): ?float
{
return $this->cvsHeaHct;
}
/**
* @param float|null $cvsHeaHct
*/
public function setCvsHeaHct(?float $cvsHeaHct): void
{
$this->cvsHeaHct = $cvsHeaHct;
}
/**
* @return float|null
*/
public function getCvsHeaHb(): ?float
{
return $this->cvsHeaHb;
}
/**
* @param float|null $cvsHeaHb
*/
public function setCvsHeaHb(?float $cvsHeaHb): void
{
$this->cvsHeaHb = $cvsHeaHb;
}
/**
* @return float|null
*/
public function getCvsNumDay(): ?float
{
return $this->cvsNumDay;
}
/**
* @param float|null $cvsNumDay
*/
public function setCvsNumDay(?float $cvsNumDay): void
{
$this->cvsNumDay = $cvsNumDay;
}
/**
* @return DateTime|null
*/
public function getCvsDay(): ?DateTime
{
return $this->cvsDay;
}
/**
* @param DateTime|null $cvsDay
*/
public function setCvsDay(?DateTime $cvsDay): void
{
$this->cvsDay = $cvsDay;
}
/**
* @return string|null
*/
public function getCvsLasId(): ?string
{
return $this->cvsLasId;
}
/**
* @param string|null $cvsLasId
*/
public function setCvsLasId(?string $cvsLasId): void
{
$this->cvsLasId = $cvsLasId;
}
/**
* @return string|null
*/
public function getCvsOxfId(): ?string
{
return $this->cvsOxfId;
}
/**
* @param string|null $cvsOxfId
*/
public function setCvsOxfId(?string $cvsOxfId): void
{
$this->cvsOxfId = $cvsOxfId;
}
/**
* @return string
*/
public function getCvsIde(): string
{
return $this->cvsIde;
}
/**
* @param string $cvsIde
*/
public function setCvsIde(string $cvsIde): void
{
$this->cvsIde = $cvsIde;
}
public function getCvsVts()
{
return $this->cvsVts;
}
/**
* @param Collection $cvsVts
*/
public function setCvsVts(ArrayCollection $cvsVts): void
{
$this->cvsVts = $cvsVts;
}
public function getCvsUlt()
{
return $this->cvsUlt;
}
/**
* @param Collection $cvsUlt
*/
public function setCvsUlt(ArrayCollection $cvsUlt): void
{
$this->cvsUlt = $cvsUlt;
}
public function addCvsUlt(CvsUlt $item)
{
$this->cvsUlt[]=$item;
$item->setCvsIde($this);
return $this;
}
public function addCvsVts(CvsVts $item)
{
$this->cvsVts[]=$item;
$item->setCvsIde($this);
return $this;
}
public function removeCvsUlt($exp)
{
$this->cvsUlt->removeElement($exp);
}
public function removeCvsVts($exp)
{
$this->cvsVts->removeElement($exp);
}
}
CvsVts
<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* CvsVts
*
* @ORMTable(name="CVS_VTS", uniqueConstraints={@ORMUniqueConstraint(name="id", columns={"id"})})
* @ORMEntity
*/
class CvsVts
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var float|null
*
* @ORMColumn(name="VTS_REC_VAS", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsRecVas;
/**
* @var float|null
*
* @ORMColumn(name="VTS_REC", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsRec;
/**
* @var float|null
*
* @ORMColumn(name="VTS_NIC", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsNic;
/**
* @var float|null
*
* @ORMColumn(name="VTS_LAC", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsLac;
/**
* @var float|null
*
* @ORMColumn(name="VTS_OXY", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsOxy;
/**
* @var float|null
*
* @ORMColumn(name="VTS_SAT", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsSat;
/**
* @var float|null
*
* @ORMColumn(name="VTS_RR", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsRr;
/**
* @var float|null
*
* @ORMColumn(name="VTS_PP", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsPp;
/**
* @var float|null
*
* @ORMColumn(name="VTS_MAP", type="float", precision=10, scale=0, nullable=true)
*/
private $vtsMap;
/**
* @var string|null
*
* @ORMColumn(name="VTS_BP", type="string", length=255, nullable=true)
*/
private $vtsBp;
/**
* @var DateTime|null
*
* @ORMColumn(name="VTS_TIM", type="datetime", nullable=true)
*/
private $vtsTim;
/**
* @var DateTime|null
*
* @ORMColumn(name="VTS_DAT", type="datetime", nullable=true)
*/
private $vtsDat;
/**
* @ORMManyToOne(targetEntity="CrfCvs",cascade={"persist"})
* @ORMJoinColumn(name="CVS_IDE", referencedColumnName="CVS_IDE")
*/
private $cvsIde;
public function __construct()
{
$this->cvsIde = new CrfCvs();
}
/**
* @return float|null
*/
public function getVtsRecVas(): ?float
{
return $this->vtsRecVas;
}
/**
* @param float|null $vtsRecVas
*/
public function setVtsRecVas(?float $vtsRecVas): void
{
$this->vtsRecVas = $vtsRecVas;
}
/**
* @return float|null
*/
public function getVtsRec(): ?float
{
return $this->vtsRec;
}
/**
* @param float|null $vtsRec
*/
public function setVtsRec(?float $vtsRec): void
{
$this->vtsRec = $vtsRec;
}
/**
* @return float|null
*/
public function getVtsNic(): ?float
{
return $this->vtsNic;
}
/**
* @param float|null $vtsNic
*/
public function setVtsNic(?float $vtsNic): void
{
$this->vtsNic = $vtsNic;
}
/**
* @return float|null
*/
public function getVtsLac(): ?float
{
return $this->vtsLac;
}
/**
* @param float|null $vtsLac
*/
public function setVtsLac(?float $vtsLac): void
{
$this->vtsLac = $vtsLac;
}
/**
* @return float|null
*/
public function getVtsOxy(): ?float
{
return $this->vtsOxy;
}
/**
* @param float|null $vtsOxy
*/
public function setVtsOxy(?float $vtsOxy): void
{
$this->vtsOxy = $vtsOxy;
}
/**
* @return float|null
*/
public function getVtsSat(): ?float
{
return $this->vtsSat;
}
/**
* @param float|null $vtsSat
*/
public function setVtsSat(?float $vtsSat): void
{
$this->vtsSat = $vtsSat;
}
/**
* @return float|null
*/
public function getVtsRr(): ?float
{
return $this->vtsRr;
}
/**
* @param float|null $vtsRr
*/
public function setVtsRr(?float $vtsRr): void
{
$this->vtsRr = $vtsRr;
}
/**
* @return float|null
*/
public function getVtsPp(): ?float
{
return $this->vtsPp;
}
/**
* @param float|null $vtsPp
*/
public function setVtsPp(?float $vtsPp): void
{
$this->vtsPp = $vtsPp;
}
/**
* @return float|null
*/
public function getVtsMap(): ?float
{
return $this->vtsMap;
}
/**
* @param float|null $vtsMap
*/
public function setVtsMap(?float $vtsMap): void
{
$this->vtsMap = $vtsMap;
}
/**
* @return string|null
*/
public function getVtsBp(): ?string
{
return $this->vtsBp;
}
/**
* @param string|null $vtsBp
*/
public function setVtsBp(?string $vtsBp): void
{
$this->vtsBp = $vtsBp;
}
/**
* @return DateTime|null
*/
public function getVtsTim(): ?DateTime
{
return $this->vtsTim;
}
/**
* @param DateTime|null $vtsTim
*/
public function setVtsTim(?DateTime $vtsTim): void
{
$this->vtsTim = $vtsTim;
}
/**
* @return DateTime|null
*/
public function getVtsDat(): ?DateTime
{
return $this->vtsDat;
}
/**
* @param DateTime|null $vtsDat
*/
public function setVtsDat(?DateTime $vtsDat): void
{
$this->vtsDat = $vtsDat;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
public function getCvsIde()
{
return $this->cvsIde;
}
public function setCvsIde(CrfCvs $cvsIde)
{
$this->cvsIde = $cvsIde;
}
}
CvsUlt
<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* CvsUlt
*
* @ORMTable(name="CVS_ULT", uniqueConstraints={@ORMUniqueConstraint(name="id", columns={"id"})})
* @ORMEntity
*/
class CvsUlt
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string|null
*
* @ORMColumn(name="ULT_ISS", type="string", length=255, nullable=true)
*/
private $ultIss;
/**
* @var float|null
*
* @ORMColumn(name="ULT_PLE_EFF", type="float", precision=10, scale=0, nullable=true)
*/
private $ultPleEff;
/**
* @var float|null
*
* @ORMColumn(name="ULT_ASC", type="float", precision=10, scale=0, nullable=true)
*/
private $ultAsc;
/**
* @var string|null
*
* @ORMColumn(name="ULT_NAM", type="string", length=255, nullable=true)
*/
private $ultNam;
/**
* @var float|null
*
* @ORMColumn(name="ULT_TIM", type="float", precision=10, scale=0, nullable=true)
*/
private $ultTim;
/**
* @var DateTime|null
*
* @ORMColumn(name="ULT_DAT", type="datetime", nullable=true)
*/
private $ultDat;
/**
* @ORMManyToOne(targetEntity="CrfCvs",cascade={"persist"},inversedBy="cvsUlt")
* @ORMJoinColumn(name="CVS_IDE", referencedColumnName="CVS_IDE")
*/
private $cvsIde;
public function __construct()
{
$this->cvsIde = new CrfCvs();
}
/**
* @return number
*/
public function getId()
{
return $this->id;
}
public function getUltIss()
{
return $this->ultIss;
}
public function getUltPleEff()
{
return $this->ultPleEff;
}
public function getUltAsc()
{
return $this->ultAsc;
}
public function getUltNam()
{
return $this->ultNam;
}
public function getUltTim()
{
return $this->ultTim;
}
public function getUltDat()
{
return $this->ultDat;
}
public function getCvsIde()
{
return $this->cvsIde;
}
public function setId($id)
{
$this->id = $id;
}
public function setUltIss($ultIss)
{
$this->ultIss = $ultIss;
}
public function setUltPleEff($ultPleEff)
{
$this->ultPleEff = $ultPleEff;
}
public function setUltAsc($ultAsc)
{
$this->ultAsc = $ultAsc;
}
public function setUltNam($ultNam)
{
$this->ultNam = $ultNam;
}
public function setUltTim($ultTim)
{
$this->ultTim = $ultTim;
}
public function setUltDat($ultDat)
{
$this->ultDat = $ultDat;
}
public function setCvsIde($cvsIde)
{
$this->cvsIde = $cvsIde;
}
}
我的CrfCvsType是这样的:
<?php
namespace AppForm;
use AppEntityCrfCvs;
use ICanBoogieInflector;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use SymfonyComponentOptionsResolverOptionsResolver;
use function ICanBoogiedowncase;
class CrfCvsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$binary = array('Yes' => 1, 'No' => 0);
$inflector = Inflector::get();
$this->comments = $options['comments'];
foreach ($this->comments as $comment) {
if ($comment->getName() !== "CVS_IDE") {
switch ($comment->getName()) {
case 'CVS_END_DAT':
case 'CVS_LAS_ID':
case 'CVS_OXF_ID':
case 'CVS_NUM_DAY':
case 'CVS_HEA_HB':
case 'CVS_HEA_HCT':
case 'CVS_END_RHI':
case 'CVS_END_ISS':
case 'CVS_DAY':
$builder
->add($inflector->camelize(downcase($comment->getName()), Inflector::DOWNCASE_FIRST_LETTER), null, array('label' => $comment->getComment(), 'required' => true));
break;
case 'CVS_BLO_DRA':
case 'CVS_ECG':
$builder
->add(
$inflector->camelize(downcase($comment->getName()), Inflector::DOWNCASE_FIRST_LETTER),
ChoiceType::class, array(
'label' => $comment->getComment(),
'required' => false,
'choices' => $binary,
'placeholder' => 'Choisir une valeur',
)
);
break;
}
}
}
$builder->add('cvsVts', CollectionType::class, [
'entry_type' => CvsVtsType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'prototype' => true,
'by_reference' => false
]);
$builder->add('cvsUlt', CollectionType::class, [
'entry_type' => CvsUltType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'prototype' => true,
'by_reference' => false
]);
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
/** @var CrfCvs */
$crf_cvs = $event->getData();
//dd($crf_cvs);
});
$builder->add('Enregistrer',SubmitType::class);
//->add('cvsUlt')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CrfCvs::class,
'comments' => null,
]);
}
}
我使用了带有宏和javascript的嵌入式表单来生成表单。在这一步中,所有操作都很好,但当我尝试提交表单时,只有CvsUlt相关的对象是持久的,CvsVts向我显示此错误:
ORMExceptionHTTP 500内部服务器错误App\Entity\CrfCvs类型的实体缺少字段"cvsIde"的指定ID。此实体的标识符生成策略要求在调用EntityManager#persist((之前填充ID字段。如果您想要自动生成标识符,则需要相应地调整元数据映射。
控制器代码:
public function new(Request $request, CrfCvsRepository $crfCvsRepository, SequenceGenerator $sequenceGenerator)
{
$this->denyAccessUnlessGranted('ROLE_SAISIE', null, 'Unable to access this page!');
//On recupère les commentaires de la table pour Afichage
//--------------------------------------------------------
$columns = $crfCvsRepository->getColumsComments();
$pat = $request->attributes->get('pat');
$crfcvs = new CrfCvs();
$crf_cvs_form = $this->createForm(CrfCvsType::class, $crfcvs, array(
'comments' => $columns
));
//dd($crf_cvs_form);
//dd($request);
$crf_cvs_form->handleRequest($request);
if ($crf_cvs_form->isSubmitted() && $crf_cvs_form->isValid()) {
//dd($request);
//Génération de la clé primaire de la table
$em = $this->getDoctrine()->getManager();
//---------------------------------------------------------------------------
if ($lastid_value = $crfCvsRepository->findLast()) {
$crfcvs->setCvsIde($sequenceGenerator->getNextKey($lastid_value[0]->getCvsIde()));
} else {
$crfcvs->setCvsIde("CVS_00000001");
}
$crfcvs->setCvsLasId($pat);
//---------------------------------------------------------------------------
//dd($crfcvs);
$em->persist($crfcvs);
$em->flush();
return $this->redirectToRoute('patient', ['pat' => $this->get('session')->get('pat')]);
}
return $this->render('crf_cvs/new.html.twig', [
'crf_cvs' => $crfcvs,
'form' => $crf_cvs_form->createView(),
'pat' => $pat,
'comments' => $columns
]);
}
请帮我解决这个问题。
您需要从cvsIde中更改策略注释。
/**
* @ORMColumn(name="CVS_IDE", type="string", length=255, nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
链接:https://github.com/KnpLabs/DoctrineBehaviors/issues/45