我試圖更新一個網站(這是基於synfony2
)和一個數據庫。我在名爲'serial_nr
'的數據庫中創建了一個新列,並將其輸入到實體文件中。現在,當我嘗試將此列添加到formbuilder我得到這個錯誤:Symfony2添加一個表單域錯誤
Method "serial_nr" for object "Symfony\Component\Form\FormView" does not exist in
AppBundle:Product:list.html.twig at line 31
這是形式:
<div class="control-group">
<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>Name</label>
<div class="controls">
{{form_widget(form.name)}}
{{form_errors(form.name)}}
</div>
</div>
<div class="control-group">
<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>Serial number</label>
<div class="controls">
{{form_widget(form.serial_nr)}} <--------- line 31
{{form_errors(form.serial_nr)}}
</div>
</div>
<div class="control-group">
<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>Price</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on">€</span>
{{form_widget(form.price)}}
</div>
{{form_errors(form.price)}}
</div>
</div>
這裏是實體:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @var varchar $serial_nr
*/
private $serial_nr;
/**
* Set serial_nr
*
* @param varchar $serial_nr
* @return ProductRevision
*/
public function setserial_nr($serial_nr)
{
$this->serial_nr = $serial_nr;
return $this;
}
/**
* Get serial_nr
*
* @return varchar
*/
public function getserial_nr()
{
return $this->serial_nr;
}
- --------------編輯----------------
控制器操作:
public function addAction(Request $request){
$product = new Product();
$form = $this->createForm(new ProductType($this->get('Doctrine')->getEntityManager()), $product);
$errors = array();
if($request->isMethod("POST")){
$form->bind($request);
$revision = new ProductRevision();
$revision->setPrice($form->get("price")->getData());
$revision->setBuyprice($form->get("buyprice")->getData());
$revision->setUnit($form->get("unit")->getData());
$revision->setStock($form->get("stock")->getData());
$revision->setDescription($form->get("description")->getData());
$revision->setDate(new DateTime(date("Y-m-d H:i:s")));
$revision->setSerial_nr($form->get("serial_nr")->getData());
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository('AppBundle:Category')->findOneBy(array('name' => $form->get("category")->getData()));
// Make sure a valid category is entered!
if($category == null){
return $this->redirect("/categories/" . $_POST["product"]["categoryId"] . "/products");
}
$revision->setProduct($product);
$product->addProductRevision($revision);
$product->setCategory($category);
$validator = $this->get('validator');
$errors = $validator->validate($revision);
if($form->isValid() && count($errors) === 0){
// Form is valid!
$em = $this->getDoctrine()->getManager();
$em->persist($revision);
$em->persist($product);
try{
$em->flush();
} catch(Exception $e){
return new Response($e->getMessage(), 200);
}
return $this->redirect("/products/" . $product->getId() . "/show");
}
}
return $this->render('AppBundle:Product:addproduct.html.twig', array('form' => $form->createView(), 'active' => "products", 'errors' => $errors));
}
----------------解決方案----------------
有一個錯字在$ builder->添加serial_nr的名稱。 這究竟是怎麼回事:
$builder->add("sreial_nr", "text", array('mapped' => false, "label" => "serial_nr"));
這是它如何工作的:
$builder->add("serial_nr", "text", array('mapped' => false, "label" => "serial_nr"));
的解決辦法是在你的控制器動作。我們可以看到它嗎? – scoolnico