freezy-bee / doctrine-form-mapper
Forms mapping of Doctrine ORM entities for Nette Framework
Installs: 13 646
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 7
Forks: 2
Open Issues: 0
Requires
- php: ~7.4 || ^8.0
- doctrine/orm: ^2.18 || ^3.0
- nette/di: ~2.4 || ^3.0
- nette/forms: ~2.4 || ^3.0
- symfony/property-access: ~3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- latte/latte: ~2.5 || ^3.0
- nette/application: ~2.4 || ^3.0
- nette/bootstrap: ~2.4 || ^3.0
- nette/tester: ~2.0
- phpstan/phpstan: ^1.0
- ramsey/uuid-doctrine: ^2.0
- symplify/easy-coding-standard: ^12.0
- tracy/tracy: ^2.5
This package is auto-updated.
Last update: 2024-11-04 21:16:28 UTC
README
Installation
The best way to install FreezyBee/DoctrineFormMapper is using Composer:
$ composer require freezy-bee/doctrine-form-mapper
config.neon
extensions: formMapper: FreezyBee\DoctrineFormMapper\DI\FormMapperExtension # optional settings formMapper: mappers: - CustomMapper() entityManager: @customEntityManager # default autowired EntityManagerInterface
Extension is responsible for registering service FreezyBee\DoctrineFormMapper\DoctrineFormMapper, you can autowire it.
Documentation
Usage
use FreezyBee\DoctrineFormMapper\DoctrineFormMapper; use FreezyBee\DoctrineFormMapper\IComponentMapper; class XPresenter 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; } }