D
D
Dmitry2021-11-03 15:53:52
symfony
Dmitry, 2021-11-03 15:53:52

What is the difference between ModelTransformer and ViewTransformer?

Hello. Actually, what is the difference between these two transformers?

Example: add the test field to the form:

$builder->add('test', TextType::class, [
    'mapped' => false,
])


Transformer code:
spoiler

public function transform($value)
{
    return $value;
}

public function reverseTransform($value)
{
    if (!$value) {
        return null;
    }

    return $this->entityManager
        ->getRepository(Test::class)
        ->find($value);
}



I can add both ModelTransformer and ViewTransformer to the test field, and the output in $form->get('test')->getData() will be an object (in the input we write the id of the Test entity) that can be found by value in this field ( entity Test), that is, both methods do the same thing. What is the difference between these methods?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flying, 2021-11-03
@dmitrydeco

Information about this is in the documentation .
In short, the difference lies in how the result of the transformation will be used.

  • ModelTransformerconverts form element data to be stored in the model or retrieved data from the model into a form,
  • ViewTransformerconverts form element data for rendering to a view or getting data from a request to a form,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question