Symfony - Value isn't inserted into the database

348 Views Asked by At

I added this code to current Entity, and made a Doctrine schema update:

/**
     * @var string
     *
     * @ORM\Column(name="ftt_code", type="string", nullable=true)
  */
  private $fttCode;

  /**
     * @return string
  */
  public function getFttCode()
  {
      return $this->fttCode;
  }

  /**
     * @param string $fttCode
  */
  public function setFttCode($fttCode)
  {
    $this->fttCode = $fttCode;
  } 

I also added this to a form builder:

 ->add('fttCode', TextType::class, ['label' => 'FTT Kód zóny', 'required' => false])

But when I insert a record into the db, this value is not inserted and it's null.

Does anyboby know where's the problem?

Edit:
Here's controller code

if ($form->isSubmitted() && $form->isValid()) {
        $security = $this->get('app.be_security.service');
        if ($security->isAdmin() === false) {
            $providers = $security->getReferenceListByClass(Provider::class);
            if(count($providers) == 1) {
                $zone->setProvider($providers);
            }
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($zone);
        $em->flush();

        $this->addFlash('notice', $this->get('translator')->trans("forms.message.inserted"));
        if($zone->isStreetDefinedZones()) {
            return $this->redirectToRoute('zone_edit', ["id" => $zone->getId()]);
        } else {
            return $this->redirectToRoute('zone_index');
        }
    }
1

There are 1 best solutions below

0
Martin M. On

If you don't already have it, add

'data_class' => YourEntity::class

to the options resolver in your form type.

Then replace

$em->persist($zone);

with

$em->persist($form->getData());