我想要从数组中更新行设置新值,现在逐个设置aribute:
$em = $this->getDoctrine()->getManager();
$company = $em->getRepository('CatalogWebBundle:ComCompany')
->findOneBy(
array('cmpCode' => $id)
);
$company->setCmpName($_POST['name']);
$company->setCmpCode($_POST['code']);
$em->flush();
也许存在从数组中设置所有属性的解决方案,有些像这样吗?
$company = $_POST;
答案 0 :(得分:2)
考虑使用Symfony Form并使用如下:
<?php
$request = $this->getRequest(); // or inject Request to the action method like myAction(Request $request)
$em = $this->getDoctrine()->getManager();
$company = $em->getRepository('CatalogWebBundle:ComCompany')
->findOneBy(
array('cmpCode' => $id)
);
$form = $this->createForm('formName', $company);
$form->handleRequest($request);
if($form->isValid()) {
// if you want, you can get entity here,
// but passing an entity in 2nd param of createForm
// provides automatic binding data
$data = $form->getData();
$em->flush();
// do some action after submitting form...
return $this->redirect($this->generateUrl('companies'));
}
详细了解如何创建表单: http://symfony.com/doc/current/book/forms.html
关于在createForm('NAME_HERE', $bindObjectHere)
中注册表单作为服务以便进一步使用命名表单:
http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services