I need to map data coming in the form of an array to an entity. The data does not come from a request, but is fetched from a remote source by the application.
I want to use a form to map the data to an entity. This is how I instantiate the form as a service and pass it to my custom class (ApiResponseMapper
):
book_form_type:
class: Air\BookishBundle\Form\Type\BookType
name:
- { name: form.type, alias: book_type }
book_form:
factory_service: form.factory
factory_method: create
class: Symfony\Component\Form\Form
arguments: [@book_form_type]
api_response_mapper:
class: Air\BookishBundle\Lib\ApiResponseMapper
arguments: [@bestseller_list_form, @book_form]
In ApiResponseMapper
, I want to be able to map the data into the array:
public function mapResponse($response)
{
$form = $this->bookForm; // bookForm is a now a memeber of this class.
$form->submit($response);
}
This will not map the $response
(array data) to my Book
entity (the bookForm
has Book
class set as the data_class
). I'm starting with Symfony, and I'm really not sure how I can use this form to map the $response. The 'traditional' method would be to call something like this in a controler: $form = $this->createForm(new BookType(), $Book);
, but as you can see, this case is different.
I need to bypass handleRequest()
and call submit()
manually. How do I map the data onto the entity in this scenario?