长话短说:使用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}
Run Code Online (Sandbox Code Playgroud)
如果我通过PUT调用/ countries/{id}并传递像{"description":"Japan"}这样的json,它会修改我的国家/地区id = 1,并输入一个空的描述.
相反,如果我尝试使用此方法创建一个新实体:
/**
* Create new Countries (in batch)
* @param Request $request json request
* …Run Code Online (Sandbox Code Playgroud)