Forums › Forums › OroPlatform › OroPlatform – Programming Questions › Update custom entity data using API PUT
This topic contains 7 replies, has 2 voices, and was last updated by kelton23om 9 years, 8 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- August 4, 2014 at 9:27 am #33408
Hi oro’s guys,
I have an issues when i try to update my custom entity using a REST API.I create an entity with my own controller, my own Form type and handler.. etc..
To do that, i duplicate(almost) an existing case in the OroPlatform..My problem seems to appear during the processForm, where the form submit do not hydrate data from the Request.
Do you have any idea where that is come from ?
- CreatorTopic
- AuthorReplies
- August 4, 2014 at 1:04 pm #33409
Hello @kelton23om!
Could you please provide more details? Are you talking about REST api ? Are you working with named form ?August 5, 2014 at 1:27 am #33410Hi,
Yes i talk about a REST API.. i try to build my own REST API to expose my Own webservices dedicates to my Custom entity.
To do that, i decided to follow an example as in the NotesBundle.. and it’s work pretty well for “delete” and “get” web services, but i encouter a problem when i try to PUT or POST some data.
After some explorations i realize that the problem come from the Form process method,
the submit method to be more precise who did not do the job!1234567891011121314151617181920212223# Oro\Bundle\SoapBundle\Form\Handler\ApiFormHandler/*** Process form** @param mixed $entity* @return bool True on successful processing, false otherwise*/public function process($entity){$this->form->setData($entity);if (in_array($this->request->getMethod(), array('POST', 'PUT'))) {$this->form->submit($this->request);if ($this->form->isValid()) {$this->onSuccess($entity);return true;}}return false;}I don’t really known why, but the model Object is never hydrated by the request data..
whereas if i trace my request i can see form data in the parameter bag.i expect that explains a bit more the situation.
thanks again to spend a bit of your time answer my questions :)
August 5, 2014 at 3:23 am #33411Can you share
$this->form->getName()
,var_dump(array_keys($this->form->all()))
andvar_dump($this->request->request->all());
?August 5, 2014 at 5:29 am #33412Hi, of course here it is:
$this->form->getName()
1string 'tour_form_api' (length=13)var_dump(array_keys($this->form->all()));
1array (size=3) 0 => string 'idErp' (length=5) 1 => string 'name' (length=4) 2 => string 'driver' (length=6)var_dump($this->request->request->all());
1array (size=3) 'idErp' => string '666' (length=3) 'name' => string 'TEST' (length=4) 'driver' => string '1' (length=1)August 6, 2014 at 9:09 am #33413Hello.
Please try to use
123$.post('/api/rest/latest/YOUR_API_METHOD, {tour_form_api: {idErp: 666, name: 'TEST', driver: 1}}).always(function(res) {console.log(res);})August 7, 2014 at 2:57 am #33414Hi,
I’m not sure to do the right thing, but i get a 404 error !!!
:/
August 13, 2014 at 1:26 am #33415I found from where my issue came…
It’s because of the form service declaration and the way the RestController manage the request.
in “Oro\Bundle\SoapBundle\Controller\Api\Rest\RestController” the “fixRequestAttributes” method handle the parameters from Request.
the problem is that this method treats the request as a “GET” if the “Form” contains a name.
1234567protected function fixRequestAttributes($entity){$request = $this->container->get('request');$formName = $this->getForm()->getName();$data = empty($formName)? $request->request->all(): $request->request->get($formName);And of course in my case i put a name in my form declaration, in the “service.yml”:
12345gc.tour.form.api:class: Symfony\Component\Form\Formfactory_method: createNamedfactory_service: form.factoryarguments: [tour_form_api, "tour", null]So i just need to remove the name from the arguments like this:
123456gc.tour.form.api:class: Symfony\Component\Form\Formfactory_method: createNamedfactory_service: form.factoryarguments: [null, "tour", null]that’s it :)
I’m not really sure about the meaning of the fixRequestAttributes method but it seems to have a quite strange behavior in my opinion!
thanks again!! keep going your good work
- AuthorReplies
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.