對於我在頁面中使用的數據表(Webix數據表),我必須使用REST API。php/symfony:在POST/PUT API中檢索URL中的屬性
我的網址,例如:http://localhost:8000/trial/1
在這個頁面中,使API調用我用的是以下幾點:
save: "rest->{{ path('api_i_post') }}",
url: "rest->{{ path('erp_interventionapi_get', { trialid: trial.id })
用GET方法,我檢索試用(/試驗/ 1 ),許多幹預從數據庫加載並填充到數據表中。
有了這個數據表,我可以「添加一個新行」。它使用POST方法(保存:「rest - > {{path('api_i_post')}}」)
當我添加一個新行時,我希望能夠自動填充字段trial_id ,取決於我在數據表中添加新行的位置(for/trial/1,trial_id = 1),但我不知道如何在POST和PUT中檢索此屬性(或試用對象ID)。
我postAction:
/**
* @Rest\Post("/api_i/", name="api_i_post")
*/
public function postAction(Request $request)
{
$data = new Intervention;
$id = $request->get('id');
$action = $request->get('action');
$daadala = $request->get('daadala');
$date = $request->get('date');
$week = $request->get('week');
$infopm = $request->get('info_pm');
$comment = $request->get('comment');
$location = $request->get('location');
$trial = $request->get('trialid');
$data->setAction($action);
$data->setDaadala($daadala);
$data->setDate($date);
$data->setWeek($week);
$data->setWho($infopm);
$data->setInfoPm($comment);
$data->setComment($location);
$data->setTrial($trial);
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
$lastid = $data->getId();
$response=array("id" => $id, "status" => "success", "newid" => $lastid);
return new JsonResponse($response);
$view = View::create(array("newid" => $lastid, "id" => $id, "status" => "success"));
return $this->handleView($view);
}
而且我putAction
/**
* @Rest\Put("/api_i/{id}")
*/
public function putAction(Request $request)
{
$data = new Intervention;
$id = $request->get('id');
$action = $request->get('action');
$daadala = $request->get('daadala');
$date = $request->get('date');
$week = $request->get('week');
$infopm = $request->get('info_pm');
$comment = $request->get('comment');
$location = $request->get('location');
$sn = $this->getDoctrine()->getManager();
$intervention = $this->getDoctrine()->getRepository('ErpBundle:Sponsor')->find($id);
if (empty($intervention)) {
return new View("Sponsor not found", Response::HTTP_NOT_FOUND);
}
$intervention->setAction($action);
$intervention->setDaadala($daadala);
$intervention->setDate($date);
$intervention->setWeek($week);
$intervention->setWho($infopm);
$intervention->setInfoPm($comment);
$intervention->setComment($location);
$sn->flush();
$response=array("id" => $id, "status" => "success");
return new JsonResponse($response);
}
你能幫我這個問題?
謝謝我的代碼非常
更新的回信後:
我有更新這在我的樹枝模板:
節省了:「休息 - > {{路徑('api_i_post',{trialid:trial。ID})}}」,
如果我期待在Ajax請求的探查,我看到它是在這裏:
核心價值
trialid 「1」
但我仍然不知道如何得到它在我的發佈請求(trial_id現在仍然是空的)
我試過了fol降脂:
/**
* @Rest\Post("/api_i/", name="api_i_post")
* @Rest\RequestParam(name="trialid")
*
* @param ParamFetcher $paramFetcher
* @param Request $request
*/
public function postAction(Request $request, ParamFetcher $paramFetcher)
{
$data = new Intervention;
$id = $request->get('id');
$action = $request->get('action');
$daadala = $request->get('daadala');
$date = $request->get('date');
$week = $request->get('week');
$infopm = $request->get('info_pm');
$comment = $request->get('comment');
$location = $request->get('location');
$trial = $paramFetcher->get('trialid');
$data->setAction($action);
$data->setDaadala($daadala);
$data->setDate($date);
$data->setWeek($week);
$data->setWho($infopm);
$data->setInfoPm($comment);
$data->setComment($location);
$data->setTrial($trial);
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
$lastid = $data->getId();
$response=array("id" => $id, "status" => "success", "newid" => $lastid);
return new JsonResponse($response);
$view = View::create(array("newid" => $lastid, "id" => $id, "status" => "success"));
return $this->handleView($view);
}
感謝您的回覆!我認爲我以這種方式檢索的ID是數據庫干預的ID。我正在嘗試獲取數據庫試用版的ID(數據庫干預中的關係trial_id)。試用標識是url中的標識(或本頁的試用對象),這是我在發佈請求時難以添加的標識。任何線索? – jonibegoode
裏面放了例如我已經在聲明參數裏面加了$ id自動獲取的@jonibegoode –