我按照官方最新的pdf手册(en Edition)做了一个添加、浏览、更新、删除产品的demo,product里面有一个多对一的category属性,经过之前测试,是没有问题的。
src/Acme/StoreBundle/Entity/Product.php
/** * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category;
src/Acme/StoreBundle/Entity/Category.php
/** * @ORM\OneToMany(targetEntity="Product", mappedBy="category") */ protected $products;
然后按照Form章节,我把添加产品的页面改造成了个表单提交添加产品:
public function createAction(Request $request) { $category = $this->getDoctrine() ->getRepository('AcmeStoreBundle:Category') ->find(1); $product = new Product(); $product->setCategory($category); $form = $this->createFormBuilder($product) ->add('name') ->add('price') ->add('description') ->getForm(); if($request->getMethod() == 'POST') { $form->bindRequest($request); if($form->isValid()) { $created = true; $em = $this->getDoctrine()->getEntityManager(); $em->persist($product); $em->flush(); } } return $this->render('AcmeStoreBundle:Default:create.html.twig', array('created' => $created, 'form' => $form->createView(), 'product_id' => $product->getId() )); }
到此时所有的环节都可以正常的工作,数据库也可以创建成功数据,然后我看到 “Creating Form Classes”,于是用命令行自带的 doctrine:generate:form 创建了个Form类:
php app/console doctrine:generate:form AcmeStoreBundle:Product
src/Acme/StoreBundle/Form/ProductType.php
namespace Acme\StoreBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class ProductType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('name') ->add('price') ->add('description') ->add('category') ; } public function getName() { return 'acme_storebundle_producttype'; } }
然后按照手册上,修改了控制器的代码:
... use Acme\StoreBundle\Form\ProductType; ... $form = $this->createForm(new ProductType(), $product); ...
但是清除缓存之后访问地址出现了symfony2的500错误,日志中的错误提示:
[2012-08-26 16:00:27] request.INFO: Matched route "store_create" (parameters: "_controller": "Acme\StoreBundle\Controller\DefaultController::createAction", "_route": "store_create") [] [] [2012-08-26 16:00:27] request.CRITICAL: Symfony\Component\Form\Exception\FormException: Entity "Acme\StoreBundle\Entity\Category" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). (uncaught exception) at /work/projects/admin/php/vendor/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php line 188 [] []
echo输出内容调试之后定位在修改的那句 “ $form = $this->createForm(new ProductType(), $product); ”
迷茫2017-05-16 16:48:23
这个报错的意思是,Symfony在生成下拉菜单(<select>)时,不知道应该把什么作为选项(<option>)的文字内容,解决办法在报错信息里已经提供了,假设你要显示的文字是categoryName:
(一)在你的Entity类里增加一个“__toString()”方法;
public function __toString() { return $this->categoryName; }
(二)指定Entity里某个字段作为option文字:
$builder ->add('name') ->add('price') ->add('description') ->add('category', 'entity', array( // ... 'property' => 'categoryName' // ... )) ;