错误:必须管理传递到选择字段的实体

时间:2019-05-26 21:25:23

标签: php symfony twig

我有一个Symfony 4项目,该项目在我的主页上有一个快速预订表格,在这里我必须选择一个日期(DateType)和一个要约(Offre实体的EntityType)。

提交表单并使其有效后,我会序列化ReservationRapideModel并将其存储在会话中。

然后,我将用户重定向到再次包含这两个字段的预订页面(因此在新控制器中)。如果要在会话中保留我的ReservationRapideModel,我想预填充它们,但是出现此错误:

  

传递给选择字段的类型为“ App \ Entity \ Offre”的实体必须进行管理。也许您忘记将其保留在实体管理器中?

我的Offre实体是参考数据,因此我不需要在数据库中注册它,而在预订表中只有一个外键。 它的属性只是一个ID和一个标签。

我的快速预订表格模型:

<?php

declare(strict_types = 1);

namespace App\Model;

use App\Entity\Offre;
use App\Helper\Traits\Entity\General\TraitEntityGeneralId;
use DateTime;
use Symfony\Component\Validator\Constraints as Assert;

class ReservationRapideModel
{
    use TraitEntityGeneralId;

    /**
     * @Assert\DateTime()
     * @Assert\GreaterThanOrEqual("tomorrow")
     * @Assert\NotBlank()
     *
     * @var DateTime|null
     */
    protected $dateDebut;

    /**
     * @var Offre|null
     */
    protected $offre;

    public function __construct()
    {
        $this->dateDebut = new DateTime('tomorrow');
    }

    // getters and setters here
}

及其形式类型:

<?php

declare(strict_types = 1);

namespace App\Form;

use App\Entity\Offre;
use App\Model\ReservationRapideModel;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ReservationRapideType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('dateDebut', DateTimeType::class, [
                'attr'   => [
                    'placeholder' => 'Date et heure',
                    'class'       => 'form-control datetimepicker-input search_form_select',
                ],
                'widget' => 'single_text',
                'format' => 'dd/MM/yyyy',
                'html5'  => false,
                'label'  => false,
            ])
            ->add('offre', EntityType::class, [
                'class'        => Offre::class,
                'multiple'     => false,
                'placeholder'  => 'Choix de l\'offre',
                'label'        => false,
                'choice_label' => function (Offre $offre) {
                    return $offre->__toString();
                },
                'attr'         => [
                    'class' => 'search_form_select',
                ],
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Réserver',
                'attr'  => [
                    'class' => 'search_bar_button',
                ],
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
                'data_class' => ReservationRapideModel::class,
            ]
        );
    }
}

第一个控制器:

<?php

declare(strict_types = 1);

namespace App\Controller;

use App\Form\ReservationRapideType;
use App\Model\ReservationRapideModel;
use App\Service\ServiceSerializer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/partial", name="site_partial_")
 *
 * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')")
 */
class PartialController extends BaseController
{
    /**
     * @Route("/reservation-rapide", name="reservation_rapide", methods={"POST"})
     */
    public function reservationRapide(Request $request, ServiceSerializer $serviceSerializer)
    {
        $session = $request->getSession();

        $reservationRapide = new ReservationRapideModel();
        if ($session !== null && $session->get('reservation_rapide') !== null) {
            $string            = $session->get('reservation_rapide');
            $reservationRapide = $this->serviceSerializer->deserializeJson($string, ReservationRapideModel::class);
        }

        $form = $this->createForm(ReservationRapideType::class, $reservationRapide);
        $form->handleRequest($request);

        if ($form->isSubmitted() === true) {
            if ($form->isValid() === true) {
                $session = $request->getSession();

                $string = $serviceSerializer->serializeJson($reservationRapide);

                if ($session !== null) {
                    $session->set('reservation_rapide', $string);
                }

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

            $this->addFlash('danger', 'Le formulaire comporte des erreurs.');

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

        return $this->render('site/layout/partials/reservation_rapide.html.twig', [
            'form'              => $form->createView(),
            'reservationRapide' => $reservationRapide,
        ]);
    }
}

第二个控制器:

<?php

declare(strict_types = 1);

namespace App\Controller;

use App\Entity\Departement;
use App\Entity\Paiement;
use App\Entity\Planification;
use App\Entity\Prestataire;
use App\Entity\Reservation;
use App\Form\ReservationType;
use App\Model\ReservationRapideModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route(name="site_reservation_")
 *
 * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')")
 */
class ReservationController extends BaseController
{
    /**
     * @Route("/reservation", name="index", methods={"GET", "POST"})
     *
     * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')")
     */
    public function index(Request $request): Response
    {
        $session = $request->getSession();

        $reservation = new Reservation();
        if ($session !== null && $session->get('reservation_rapide') !== null) {
            $string            = $session->get('reservation_rapide');
            $reservationRapide = $this->serviceSerializer->deserializeJson($string, ReservationRapideModel::class);

            if ($reservationRapide instanceof ReservationRapideModel) {
                $planification = new Planification();
                $planification->setOffre($reservationRapide->getOffre());
                $planification->setDateDebut($reservationRapide->getDateDebut());

                $reservation->setPlanification($planification);
            }
        }

        $formReservation = $this->createForm(ReservationType::class, $reservation);
        $formReservation->handleRequest($request);

此行第二个控制器中有错误:

$formReservation = $this->createForm(ReservationType::class, $reservation);

0 个答案:

没有答案