Symfony 4 ORMException

问题描述 投票:0回答:1

您好,我遇到此异常:

类型为App \ Entity \ CrfCvs的实体缺少为字段'cvsIde'分配的ID。该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果要自动生成标识符,则需要相应地调整元数据映射。

我有一个CrfCvs对象,它具有两个相关的对象:CvsVts和CvsUtl像这样:CrfCvs

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * CrfCvs
 *
 * @ORM\Table(name="CRF_CVS")
 * @ORM\Entity
 */
class CrfCvs
{
    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_ECG", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsEcg;

    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_BLO_DRA", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsBloDra;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CVS_END_ISS", type="string", length=255, nullable=true)
     */
    private $cvsEndIss;

    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_END_RHI", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsEndRhi;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="CVS_END_DAT", type="datetime", nullable=true)
     */
    private $cvsEndDat;

    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_HEA_HCT", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsHeaHct;

    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_HEA_HB", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsHeaHb;

    /**
     * @var float|null
     *
     * @ORM\Column(name="CVS_NUM_DAY", type="float", precision=10, scale=0, nullable=true)
     */
    private $cvsNumDay;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="CVS_DAY", type="datetime", nullable=true)
     */
    private $cvsDay;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CVS_LAS_ID", type="string", length=255, nullable=true)
     */
    private $cvsLasId;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CVS_OXF_ID", type="string", length=255, nullable=true)
     */
    private $cvsOxfId;

    /**
     * @ORM\Column(name="CVS_IDE", type="string", length=255, nullable=false)
     * @ORM\Id
     */
    private $cvsIde;


    /**
     * @ORM\OneToMany(targetEntity="App\Entity\CvsVts", mappedBy="cvsIde", cascade={"persist"})
     */
    private $cvsVts;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\CvsUlt", 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 App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * CvsVts
 *
 * @ORM\Table(name="CVS_VTS", uniqueConstraints={@ORM\UniqueConstraint(name="id", columns={"id"})})
 * @ORM\Entity
 */
class CvsVts
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")\
     */
    private $id;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_REC_VAS", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsRecVas;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_REC", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsRec;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_NIC", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsNic;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_LAC", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsLac;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_OXY", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsOxy;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_SAT", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsSat;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_RR", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsRr;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_PP", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsPp;

    /**
     * @var float|null
     *
     * @ORM\Column(name="VTS_MAP", type="float", precision=10, scale=0, nullable=true)
     */
    private $vtsMap;

    /**
     * @var string|null
     *
     * @ORM\Column(name="VTS_BP", type="string", length=255, nullable=true)
     */
    private $vtsBp;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="VTS_TIM", type="datetime", nullable=true)
     */
    private $vtsTim;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="VTS_DAT", type="datetime", nullable=true)
     */
    private $vtsDat;

    /**
     * @ORM\ManyToOne(targetEntity="CrfCvs",cascade={"persist"})
     * @ORM\JoinColumn(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 App\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * CvsUlt
 *
 * @ORM\Table(name="CVS_ULT", uniqueConstraints={@ORM\UniqueConstraint(name="id", columns={"id"})})
 * @ORM\Entity
 */
class CvsUlt
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")\
     */
    private $id;

    /**
     * @var string|null
     *
     * @ORM\Column(name="ULT_ISS", type="string", length=255, nullable=true)
     */
    private $ultIss;

    /**
     * @var float|null
     *
     * @ORM\Column(name="ULT_PLE_EFF", type="float", precision=10, scale=0, nullable=true)
     */
    private $ultPleEff;

    /**
     * @var float|null
     *
     * @ORM\Column(name="ULT_ASC", type="float", precision=10, scale=0, nullable=true)
     */
    private $ultAsc;

    /**
     * @var string|null
     *
     * @ORM\Column(name="ULT_NAM", type="string", length=255, nullable=true)
     */
    private $ultNam;

    /**
     * @var float|null
     *
     * @ORM\Column(name="ULT_TIM", type="float", precision=10, scale=0, nullable=true)
     */
    private $ultTim;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="ULT_DAT", type="datetime", nullable=true)
     */
    private $ultDat;


    /**
     * @ORM\ManyToOne(targetEntity="CrfCvs",cascade={"persist"},inversedBy="cvsUlt")
     * @ORM\JoinColumn(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 App\Form;

use App\Entity\CrfCvs;
use ICanBoogie\Inflector;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use function ICanBoogie\downcase;

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,
        ]);
    }
}

我已经使用带有宏的嵌入式Form和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 ]); }
请帮助我解决这个问题。

您好,遇到此异常:类型为App \ Entity \ CrfCvs的实体缺少为字段'cvsIde'分配的ID。该实体的标识符生成策略要求ID字段为...

php symfony doctrine-orm doctrine
1个回答
0
投票
您需要从cvsIde更改策略注释。
© www.soinside.com 2019 - 2024. All rights reserved.