Symfony2 (2.0.17-DEV) 500 error occurs after creating form type
黄舟
黄舟 2017-05-16 16:46:23
0
1
359

I made a demo of adding, browsing, updating, and deleting products according to the latest official PDF manual (en Edition). There is a many-to-one category attribute in the product. After previous testing, there is no problem.
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;

Then according to the Form chapter, I transformed the page for adding products into a form to submit to add products:

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

At this point, all links can work normally, and the database can also create successful data. Then I saw "Creating Form Classes", so I created a Form class using the doctrine:generate:form that comes with the command line:

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

Then I modified the controller code according to the manual:

... use Acme\StoreBundle\Form\ProductType; ... $form = $this->createForm(new ProductType(), $product); ...

But after clearing the cache, a symfony2 500 error occurred in the access address. The error message in the log is:

[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 [] []

After debugging the echo output content, it is located at the modified sentence "$form = $this->createForm(new ProductType(), $product);"

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all (1)
迷茫

The meaning of this error is that when Symfony generates the drop-down menu (

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!