Короче говоря: используя FOSRestBundle, я пытаюсь создать некоторые сущности с помощью вызова POST или изменить существующие с помощью PUT.
вот код:
/**
* Put action
* @var Request $request
* @var integer $id Id of the entity
* @return View|array
*/
public function putCountriesAction(Request $request, $id)
{
$entity = $this->getEntity($id);
$form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->view(null, Codes::HTTP_NO_CONTENT);
}
return array(
'form' => $form,
);
} //[PUT] /countries/{id}
Если я вызываю /countries/{id} с PUT, передавая json, например {"description":"Japan"}, он меняет мою страну с id=1, добавляя пустое описание.
Если вместо этого я попытаюсь создать НОВЫЙ объект с помощью этого метода:
/**
* Create new Countries (in batch)
* @param Request $request json request
* @return array redirect to get_coutry, will show the newly created entities
*/
public function postCountriesAction(Request $request)
{
$entity = new Countries();
$form = $this->createForm(new CountriesType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_country',
array('id' => $entity->getId())
),
Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
} //[PUT {"description":"a_description"}] /countries
это дает мне сообщение об ошибке:
exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null
поэтому кажется, что я не могу правильно передать запрос на привязку к форме.
Обратите внимание, что если я json_decode запрос, как предложено, rest-api">здесь он отвечает
{
"code":400,
"message":"Validation Failed",
"errors":{
"errors":[
"This value is not valid."
],
"children":{
"description":[
]
}
}
}
Любой совет?
Спасибо, Роллс