早上好,在symfony项目中,我需要通过表中的编辑按钮进行更新。此按钮将我带到一个输入页面,其中的值已输入的。但是,当我修改(此处示例为“Ref”)并进行验证时,数据库不会更新。
newmodif.html.twig:
<div class="refPrincipale">
Référence Principale :
<input id="1" class="champ input_uppercase input_color" maxlength="35" name="ref" value="{{ expedition.RefPrincipale }}">
</div>
<td><a type="button" class="btn bouton add_button" href="{{ path('expedition_updatee', {'id': expedition.id}) }}" >Valider</a></td>
ExpeditionController.php中的函数更新:
public function updatee(Request $request, int $id) : Response
{
$em = $this->getDoctrine()->getManager();
$expedition = $em->getRepository(Expedition::class)->findOneBy(['id' => $id]);
if (!$expedition) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
if (null !== $request->request->get("ref")) {
$expedition->setRefPrincipale($request->request->get("ref"));
}
$em->persist($expedition);
$em->flush();
return $this->redirectToRoute('expedition_encours');
}
路线.yaml
expedition_updatee:
path: /expedition/encours/{id}
controller: App\Controller\ExpeditionController::updatee
Expedition.php:
public function getRefPrincipale(): ?string
{
return $this->ref_principale;
}
public function setRefPrincipale(string $ref_principale): self
{
$this->ref_principale = $ref_principale;
return $this;
}
如果我替换该行:
$expedition->setRefPrincipale($request->request->get("ref"));
签署人:
$expedition->setRefPrincipale("change data");
它可以工作,当我单击“验证”时,数据库会更新,但这不是我想要的,因为它会因为代码而改变,而不是因为页面上修改的字段。