contributte / forms

:sparkles: Extra contrib to nette/forms (@nette)
https://contributte.org/packages/contributte/forms.html
MIT License
9 stars 4 forks source link

Date is not updated with Doctrine ORM #34

Closed petrparolek closed 1 year ago

petrparolek commented 1 year ago

Version: dev-master

Bug Description

I used https://github.com/FreezyBee/DoctrineFormMapper. In DB I have null in date column. If is filled date in form date is not updated in DB (NULL persists).

Steps To Reproduce

Example presenter:

    use FreezyBee\DoctrineFormMapper\DoctrineFormMapper;
    use FreezyBee\DoctrineFormMapper\IComponentMapper;

    class SomePresenter extends Presenter
    {
        /** @var DoctrineFormMapper @inject */
        public $mapper;

        /** @var EntityRepository @inject */
        public $articlesRepository;

        protected function createComponentForm()
        {
            $form = new Form;

            // Column
            $form->addText('name');

            // ManyToOne
            $form->addSelect('author')
                // order items
                ->setOption(IComponentMapper::ITEMS_ORDER, ['age' => 'ASC'])
                // filter items
                ->setOption(IComponentMapper::ITEMS_FILTER, ['age' => 0])
                // filter items by callback
                ->setOption(IComponentMapper::ITEMS_FILTER, function(QueryBuilder $qb) {
                    $qb->andWhere('entity.age != 0')
                })
                // custom select label renderer
                ->setOption(IComponentMapper::ITEMS_TITLE, function (Author $author) {
                    return $author->getName() . ' - ' . $author->getAge();
                });

            // ManyToOne
            $form->addRadioList('tags')
                ->setOption(IComponentMapper::ITEMS_TITLE, 'name');

            // ManyToMany
            $form->addMultiSelect('users')
                ->setOption(IComponentMapper::ITEMS_TITLE, 'username');

            // ManyToMany
            // btw you can define items and then ITEMS_TITLE is not required
            $form->addCheckboxList('countries', 'Countries', [1 => 'CZ', 2 => 'SK']);

            // A) create new entity
            $article = new Article;

            // B) load entity from db
            $article = $this->articlesRepository->find(1);

            // C) create new entity by class name - see point INFO below
            $article = Article::class;

            // load data from entity to form
            $this->mapper->load($article, $form);

            $form->onSuccess[] = function (Form $form) use ($article) {

                // save (map) data from form to entity - without flush!!!
                $articleEntity = $this->mapper->save($article, $form);

                // INFO - if article was classname, mapper create new instance
                // $articleEntity is instanceof Article

                // flush data...
                $em = $this->mapper->getEntityManager();
                $em->persist($articleEntity)
                $em->flush();
            };

            return $form;
        }
    }

Example entity:

<?php

declare(strict_types=1);

namespace App\Entities;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class TestDate
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    private int $id = 0;

    /**
     * @ORM\Column(type="string")
     */
    private string $title;

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

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

    public function getTitle(): int
    {
        return $this->title;
    }

    public function getDate(): DateTime
    {
        return $this->date;
    }

   public function setTitle(string $title): void
   {
      $this->title = $title;
   }

   public function setDate(DateTime $date): void
   {
      $this->date = $date;
   }
}

Expected Behavior

Save correct data into DB

Possible Solution

PR: #33

petrparolek commented 1 year ago

I missed this https://github.com/contributte/forms/tree/c7ac26b456ff2958ea207c43be6515ad1154dcea/.docs#returned-value-type . It is OK for me.