Symfony2(2.0.17-DEV)創建form type之後出現500錯誤
黄舟
黄舟 2017-05-16 16:46:23
0
1
464

我依照官方最新的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); ”###
黄舟
黄舟

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

全部回覆(1)
迷茫

這個報錯的意思是,Symfony在生成下拉式選單(

(一)在你的Entity類別裡增加一個「__toString()」方法;

public function __toString()
{
    return $this->categoryName;
}

(二)指定Entity裡某個欄位作為option文字:

$builder
    ->add('name') 
    ->add('price') 
    ->add('description') 
    ->add('category', 'entity', array(
        // ...
        'property' => 'categoryName'
        // ...
    ))
;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板