简体   繁体   English

动态形式的Symfony数据转换器

[英]Symfony Data Transformer on dynamic form

I'm building an API using FOSRestBundle for adding products to a basket. 我正在使用FOSRestBundle构建API,以将产品添加到购物篮。 For the sake of keeping this example simple we have a range of products which come in different sizes. 为了使这个例子简单,我们提供了一系列尺寸不同的产品。

I'd like to be able to specify the size code in the JSON request. 我希望能够在JSON请求中指定大小代码。 For example: 例如:

{
    "product": 3,
    "size": "S"
}

(I'd also like to use the product code instead of the database ID, but that's for another day!) (我也想使用产品代码代替数据库ID,但这是另一天!)

Other parts of the project I have done similar tasks using data transformers, but these were simpler forms where the values didn't change based on other fields selected values. 在项目的其他部分,我使用数据转换器完成了类似的任务,但是这些形式较为简单,其中值不会根据其他字段选择的值而改变。

So my current basket form... 所以我目前的购物篮形式

class BasketAddType extends AbstractType
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('product', 'entity', [
                'class' => 'CatalogueBundle:Product',
            ]);

        $builder->get('product')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
    }

    public function onPostSubmit(FormEvent $event)
    {
        // this will be Product entity
        $form = $event->getForm();

        $this->addElements($form->getParent(), $form->getData());
    }

    protected function addElements(FormInterface $form, Product $product = null)
    {
        if (is_null($product)) {
            $sizes = [];
        } else {
            $sizes = $product->getSizes();
        }

        $form
            ->add('size', 'size', [
                'choices' => $sizes
            ]);
    }

    public function getName()
    {
        return '';
    }
}

The custom size form type I'm using above is so I can add the model transformer. 我在上面使用的自定义尺寸表格类型是,因此我可以添加模型转换器。 As found in this answer https://stackoverflow.com/a/19590707/3861815 如在此答案中找到https://stackoverflow.com/a/19590707/3861815

class SizeType extends AbstractType
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer(new SizeTransformer($this->em));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'class' => 'CatalogueBundle\Entity\Size'
        ]);
    }

    public function getParent()
    {
        return 'entity';
    }

    public function getName()
    {
        return 'size';
    }
}

And finally the transformer. 最后是变压器。

class SizeTransformer implements DataTransformerInterface
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function transform($size)
    {
        if (null === $size) {
            return '';
        }

        return $size->getCode();
    }

    public function reverseTransform($code)
    {
        // WE NEVER GET HERE?
        $size = $this->em->getRepository('CatalogueBundle:Size')
        ->findOneByCode($code);

        if (null === $size) {
            throw new TransformationFailedException('No such size exists');
        }

        return $size;
    }
}

So I did a quick exit; 所以我做了一个快速exit; in reverseTransform and it's never fired so I will always get an error on the size element about it being invalid. reverseTransform ,它从未被触发过,所以我总是在size元素上收到关于它无效的错误。

What would be the best way to getting a data transformer onto the size field here? 将数据转换器放入此处的大小字段的最佳方法是什么?

This is a dependent fields 这是一个依赖字段
I have a Product entity wich in relation with a Category entity which in relation with a Collection entity 我有一个与类别实体有关的产品实体,与一个类别实体有关的类别
here is my code for the add product form 这是我添加产品表单的代码

class ProductsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $propertyPathToCategory = 'category';
        $builder
                ->add('title')
                ->add('description','textarea')
                ->add('collection','entity', array(
                        'class' => 'FMFmBundle:Collections',
                        'empty_value'   => 'Collection',
                        'choice_label' => 'title'
                        ))
                ->addEventSubscriber(new AddCategoryFieldSubscriber($propertyPathToCategory));
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FM\FmBundle\Entity\Products'
        ));
    }
    public function getName()
    {
        return 'fm_fmbundle_products';
    }
}

the AddCategoryFieldSubscriber AddCategoryFieldSubscriber

//Form/EventListener
class AddCategoryFieldSubscriber implements EventSubscriberInterface
{
    private $propertyPathToCategory;

    public function __construct($propertyPathToCategory)
    {
        $this->propertyPathToCategory = $propertyPathToCategory;
    }

    public static function getSubscribedEvents()
    {
        return array(
                FormEvents::PRE_SET_DATA  => 'preSetData',
                FormEvents::PRE_SUBMIT    => 'preSubmit'
        );
    }

    private function addCategoryForm($form, $collection_id)
    {
        $formOptions = array(
                'class'         => 'FMFmBundle:Categories',
                'empty_value'   => 'Category',
                'label'         => 'Category',
                'attr'          => array(
                'class' => 'Category_selector',
                ),
                'query_builder' => function (EntityRepository $repository) use ($collection_id) {
                $qb = $repository->createQueryBuilder('category')
                ->innerJoin('category.collection', 'collection')
                ->where('collection.id = :collection')
                ->setParameter('collection', $collection_id)
                ;

                return $qb;
                }
                );

        $form->add($this->propertyPathToCategory, 'entity', $formOptions);
    }

    public function preSetData(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        if (null === $data) {
            return;
        }

        $accessor    = PropertyAccess::createPropertyAccessor();

        $category        = $accessor->getValue($data, $this->propertyPathToCategory);
        $collection_id = ($category) ? $category->getCollection()->getId() : null;

        $this->addCategoryForm($form, $collection_id);
    }

    public function preSubmit(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        $collection_id = array_key_exists('collection', $data) ? $data['collection'] : null;

        $this->addCategoryForm($form, $collection_id);
    }
}

add new action in the controller 在控制器中添加新动作

public function SelectCategoryAction(Request $request)
    {
        $collection_id = $request->get('collecton_id');
        $em = $this->getDoctrine()->getManager();
        $categories = $em->getRepository('FMFmBundle:Categories')->findByCollection($collection_id);
        $Jcategories=array();
        foreach($categories as $category){
            $Jcategories[]=array(
                    'id' => $category->getId(),
                    'title' => $category->getTitle()
                    );
        }
        return new JsonResponse($Jcategories);
    }

add new route for the action 为操作添加新路线

select_category:
    path:     /selectcategory
    defaults: { _controller: FMFmBundle:Product:SelectCategory }

and some ajax 和一些ajax

$("#collectionSelect").change(function(){
        var data = {
            collecton_id: $(this).val()
        };
        $.ajax({
            type: 'post',
            url: 'selectcategory',
            data: data,
            success: function(data) {
                var $category_selector = $('#categorySelect');
                $category_selector.empty();

                for (var i=0, total = data.length; i < total; i++)
                    $category_selector.append('<option value="' + data[i].id + '">' + data[i].title + '</option>');

            }
        });
    });

References: 参考文献:

Dependent Forms 相依表格

So the problem was that I was using an entity type instead of a text type when using the model data transformer. 因此,问题在于使用模型数据转换器时,我使用的是实体类型而不是文本类型。

Here is my working code albeit probably not perfect, the primary form 这是我的工作代码,尽管可能不完美,但主要形式

class BasketAddType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('product', 'entity', [
                'class' => 'CatalogueBundle:Product'
            ]);

        $builder->get('product')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
    }

    public function onPostSubmit(FormEvent $event)
    {
        $form = $event->getForm()->getParent();
        $product = $event->getForm()->getData();

        $form
            ->add('size', 'size', [
                'sizes' => $product->getSizes()->toArray() // getSizes() is an ArrayCollection
            );
    }

    public function getName()
    {
        return '';
    }
}

My custom form size type which applies the model transformer with the provided size options. 我的自定义表单尺寸类型,将模型转换器与提供的尺寸选项一起应用。

class SizeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer(new SizeTransformer($options['sizes']));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired([
            'sizes'
        ]);
    }

    public function getParent()
    {
        return 'text';
    }

    public function getName()
    {
        return 'size';
    }
}

And finally the size transformer. 最后是尺寸变压器。

class SizeTransformer implements DataTransformerInterface
{
    protected $sizes;

    public function __construct(array $sizes)
    {
        $this->sizes = $sizes;
    }

    public function transform($size)
    {
        if (null === $size) {
            return '';
        }

        return $size->getCode();
    }

    public function reverseTransform($code)
    {
        foreach ($this->sizes as $size) {
            if ($size->getCode() == $code) {
                return $size;
            }
        }

        throw new TransformationFailedException('No such size exists');
    }
}

This solution wouldn't work too well if there were a high number of sizes available for each product. 如果每种产品都有大量尺寸可用,则此解决方案将无法很好地工作。 Guess if that was the case I'd need to pass both the EntityManager and the product into the transformer and query the DB accordingly. 猜测是否是这种情况,我需要将EntityManager和产品都传递到转换器中,并相应地查询数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM