S
S
symnoob2018-05-27 21:43:11
symfony
symnoob, 2018-05-27 21:43:11

Symfony 4 - Doctrine - CRUD Update, Insert fails, why?

Good afternoon Forum,
I created 2 CRUD controllers, with the OneToOne dependency, but for some reason Update and Insert Child-Entity are not executed, can anyone tell me?
Queries: (why not commit?)

"START TRANSACTION"
Parameters:[]

INSERT INTO immo_ausstattung (backofen, objekt_id) VALUES (?, ?)
Parameters:[1 => 1  2 => 2]

"COMMIT"
Parameters:[]

entity parent:
<?php

namespace App\Entity\Account\Vermieten;

use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Locale as Locale;

/**
 * @ORM\Entity(repositoryClass="App\Repository\Account\Vermieten\ObjektRepository")
 * @ORM\Table(name="immo_miete")
 */
class Objekt
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;


    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Account\Vermieten\Ausstattung", mappedBy="objekt", cascade={"persist", "remove"})
     */
    private $ausstattung;

public function getAusstattung(): ?Ausstattung
    {
        return $this->ausstattung;
    }

    public function setAusstattung(Ausstattung $ausstattung): self
    {
        $this->ausstattung = $ausstattung;

        // set the owning side of the relation if necessary
        if ($this !== $ausstattung->getObjekt()) {
            $ausstattung->setObjekt($this);
        }

        return $this;
    }

....

child entity:
<?php

namespace App\Entity\Account\Vermieten;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\Account\Vermieten\AusstattungRepository")
 * @ORM\Table(name="immo_ausstattung")
 */
class Ausstattung
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Account\Vermieten\Objekt", inversedBy="ausstattung", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="objekt_id", referencedColumnName="id")
     */
    private $objekt;

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

    public function getId()
    {
        return $this->id;
    }

    public function getObjekt(): ?Objekt
    {
        return $this->objekt;
    }

    public function setObjekt(Objekt $objekt): self
    {
        $this->objekt = $objekt;

        return $this;
    }

    public function getBackofen(): ?bool
    {
        return $this->backofen;
    }

    public function setBackofen(?bool $backofen): self
    {
        $this->backofen = $backofen;

        return $this;
    }
}

CRUD - Controller for Child - Entity:
<?php

namespace App\Controller\Account\Vermieten;

use App\Entity\Account\Vermieten\Ausstattung;
use App\Entity\Account\Vermieten\Objekt;
use App\Form\Account\Vermieten\AusstattungType;
use App\Repository\Account\Vermieten\AusstattungRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/account/vermieten/ausstattung")
 */
class AusstattungController extends Controller
{

/**
     * @Route("/new/{id}", requirements={"id" = "\d+"}, name="account_vermieten_ausstattung_new", methods="GET|POST")
     */
    public function new(Request $request,$id): Response
    {
        $ausstattung = new Ausstattung();
        $form = $this->createForm(AusstattungType::class, $ausstattung);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $fkId = $em->getRepository(Objekt::class)->find($id);
            $ausstattung->setObjekt($fkId);
            $em->persist($ausstattung);
            $em->flush();

            #return $this->redirectToRoute('account_vermieten_ausstattung_index');
        }

        return $this->render('account/vermieten/ausstattung/new.html.twig', [
            'ausstattung' => $ausstattung,
            'form' => $form->createView(),
        ]);
    }

/**
     * @Route("/{id}/edit", name="account_vermieten_ausstattung_edit", methods="GET|POST")
     */
    public function edit(Request $request, Ausstattung $ausstattung): Response
    {
        $form = $this->createForm(AusstattungType::class, $ausstattung);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('account_vermieten_ausstattung_edit', ['id' => $ausstattung->getId()]);
        }

        return $this->render('account/vermieten/ausstattung/edit.html.twig', [
            'ausstattung' => $ausstattung,
            'form' => $form->createView(),
        ]);
    }
....
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question