douyi8760 2015-10-13 11:24
浏览 63
已采纳

Symfony2添加表单字段错误

I'm trying to update a website(which is based on synfony2) and a database. I have created a new column in the database named 'serial_nr' and entered it in the entity file. Now when i try to add this column to a formbuilder I get this error:

Method "serial_nr" for object "Symfony\Component\Form\FormView" does not exist in 
AppBundle:Product:list.html.twig at line 31

This is the form:

<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">&euro;</span>
             {{form_widget(form.price)}}
         </div>
         {{form_errors(form.price)}}
    </div>
</div>

Here is the entity:

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;
    }

----------------EDIT----------------
The controller action:

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));
}

----------------Solution----------------

There was a typo in the name of the $builder->add of serial_nr. This is how it was:

$builder->add("sreial_nr", "text", array('mapped' => false, "label" => "serial_nr"));

This is how it works:

 $builder->add("serial_nr", "text", array('mapped' => false, "label" => "serial_nr"));
  • 写回答

1条回答 默认 最新

  • duanpa1980 2015-10-13 11:35
    关注

    Should be:

    public function setSerial_nr($serial_nr)
    
    public function getSerial_nr()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统