将收集数据插入数据库

时间:2019-04-12 19:28:07

标签: php symfony doctrine-orm symfony4

我在表单中有一个CollectionType字段,并且做到了,因此我可以根据需要添加任意多个这些字段,这是一张图片(collection类型是Exception下的字段)  https://imgur.com/a/xQ7qUNT

现在,我试图遍历数据并将其插入到数组中,最后将其插入数据库中,但这就是我在数据库https://imgur.com/a/WyBmmwr中得到的内容 另外,在单击提交https://imgur.com/a/pLBKx1y之后,尝试获取咳嗽的数据。 这是我的方法:

/**
 * @Route("/new", name="type_parking_new", methods={"GET","POST"})
 */
public function new(Request $request): Response
{
    $typeParking = new TypeParking();
    $exception = new Exception();
    $form = $this->createForm(TypeParkingType::class, $typeParking);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $lesjours = $typeParking->getJourstravail();
        $typeParking->getException()->add($exception);

        // Here I try getting the data
        $excep = $form->get('Exception');
        foreach ($excep as $ExceptionForm) {
            $name = $ExceptionForm->get('nom')->getData();
            $StartDate = $ExceptionForm->get('datedebut')->getData();
            $EndDate = $ExceptionForm->get('datefin')->getData();
            $StartTime = $ExceptionForm->get('tempsdebut')->getData();
            $EndTime = $ExceptionForm->get('tempsfin')->getData();

            $exception->setNom($name);
            $exception->setDatedebut($StartDate);
            $exception->setDatefin($EndDate);
            $exception->setTempsdebut($StartTime);
            $exception->setTempsfin($EndTime);

            $typeParking->addException($exception);
        }
        // ends here
        // this is unrelated
        $jour = $lesjours['jour'];
        $debut = $lesjours['debut']->format('H:i:s');
        $fin = $lesjours['fin']->format('H:i:s');
        $newDate = Array('lesjour' => Array($jour => Array('heuredebut' => $debut, 'heurefin' => $fin)));
        $typeParking->setJourstravail($newDate);
        //end unrelated
        $this->addFlash('success', "type added ");

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($typeParking);
        $entityManager->flush();

        return $this->redirectToRoute('type_parking_index');
    }

    return $this->render(
        'Admin/type_parking/new.html.twig',
        ['type_parking' => $typeParking, 'form' => $form->createView()]
    );
}

这是我的实体TypeParking

<?php

namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass="App\Repository\TypeParkingRepository")
 */
class TypeParking
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=55)
     */
    private $libelle;

    /**
     * @ORM\Column(type="time", nullable=true)
     */
    private $tempsmax;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $jourdebut;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $jourfin;

    /**
     * @ORM\Column(type="json_array", nullable=true)
     */
    private $jourstravail;

    /**
     * @ORM\Column(type="json_array", nullable=true)
     */
    private $exception;

    public function __construct()
    {
        $this->exception = new ArrayCollection();
    }
    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTempsmax(): ?\DateTimeInterface
    {
        return $this->tempsmax;
    }

    public function setTempsmax(\DateTimeInterface $tempsmax): self
    {
        $this->tempsmax = $tempsmax;

        return $this;
    }

    public function getJourdebut(): ?\DateTimeInterface
    {
        return $this->jourdebut;
    }

    public function setJourdebut(\DateTimeInterface $jourdebut): self
    {
        $this->jourdebut = $jourdebut;

        return $this;
    }

    public function getJourfin(): ?\DateTimeInterface
    {
        return $this->jourfin;
    }

    public function setJourfin(\DateTimeInterface $jourfin): self
    {
        $this->jourfin = $jourfin;

        return $this;
    }

    public function getJourstravail()
    {
        return array_merge([
            'jour' => '', 
            'debut' => null, 
            'fin' => null, 


            // other sub-fields "empty" values
        ], $this->jourstravail ?? [] // prevent array_merge from failing if exception is empty
        );    }

    public function setJourstravail($jourstravail): self
    {
        $this->jourstravail = $jourstravail;

        return $this;
    }

    public function getException() {
        return $this->exception;
            }

    public function setException($exception): self
    {
        $this->exception = $exception;

        return $this;
    }

    public function addException($exception)
{
    $this->exception->add($exception);
    return $this;
}

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }
}

顺便说一句,我有两个实体,TypeParking和Exception,TypeParking有一个名为Exception的属性,它是一个json文件类型,必须包含Exception中的数据。

0 个答案:

没有答案
相关问题