Php 使用Symfony2和Doctrine将实体Dropdownlist持久化到数据库中

Php 使用Symfony2和Doctrine将实体Dropdownlist持久化到数据库中,php,symfony,doctrine,Php,Symfony,Doctrine,ProductType.php class ProductType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('id_seller', 'text') ->add('tag', 'text') ->add('

ProductType.php

class ProductType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('id_seller', 'text')
                ->add('tag', 'text')
                ->add('hot', 'checkbox', array(
                    'label' => 'Show this entry publicly?',
                    'required' => false))
                ->add('featured', 'checkbox')
                ->add('new', 'checkbox')
                ->add('shipping', 'checkbox')
                ->add('id_category', 'entity', array(
                    'class' => 'ProductBundle:ProductCategory',
                    'property' => 'name'
                        )
                )
                ->add('Add', 'submit');
    }

    public function getName() {

        return 'product';
    }

}
ProductRepository.php

class ProductRepository extends EntityRepository {

    public function addProduct($id_seller, $tag, $hot, $featured, $new, $shipping, $id_category, $username) {

        $date = date("Y-m-d H:i:s");

        $product = new Product();

        $product->setIdSeller($id_seller);
        $product->setTag($tag);
        $product->setHot($hot);
        $product->setFeatured($featured);
        $product->setNew($new);
        $product->setShipping($shipping);
        $product->setIdCategory($id_category);
        $product->setStatus('0');
        $product->setDateUpdate(new \Datetime($date));
        $product->setUpdatedBy($username);
        $product->setDateAdd(new \Datetime($date));

        $em = $this->_em;
        $em->persist($product);
        $em->flush();

        return true;
    }

}
控制员:

class ProductController extends Controller
{
    /**
     * @Route("/add")
     *
     */
    public function addProductAction(){

        $product = new Product();
        $form = $this->createForm(new ProductType(), $product);

        $request = $this->get('request');
        $form->handleRequest($request);

        if($request->getMethod() == "POST"){

            if($form->isValid()){

                $user = $this->get('security.context')->getToken()->getUser();
                $username = $user->getUsername();

                $id_seller = $form->get('id_seller')->getData();
                $tag = $form->get('tag')->getData();
                $hot = $form->get('hot')->getData();
                $featured = $form->get('featured')->getData();
                $new = $form->get('featured')->getData();
                $shipping = $form->get('shipping')->getData();
                $id_category = $form->get('id_category')->getData();

                $em = $this->getDoctrine()->getManager();

                $product = $em->getRepository('ProductBundle:Product')->addProduct($id_seller, $tag, $hot, $featured, $new, $shipping, $id_category, $username);
            }
        }

        return $this->render('ProductBundle:Product:addproduct.html.twig', array('form'=>$form->createView()));
    }
}
错误代码: 使用参数[123,123,1,1,1,1,0,{},0,2014-11-21 10:59:06,管理员,2014-11-21 10:59:06]:

我可以知道如何获取id_类别下拉列表的值吗?正如您从错误消息中看到的那样,我得到了{}。值应为1、2、3或4


非常感谢。

它有实体类型。您是否尝试过使用$product->setIdCategory$id\U category->getId;有没有理由不检查表单是否已发送和呼叫设置。。。每个领域?请检查,你会发现如何用更少的代码做同样的事情。我刚刚注意到你在实体库中持久化了实体。这在Symfony2中不是常见的做法。您应该从控制器执行此操作。请显示完整的代码,我们看不到ProductType在您当前的代码中是如何使用的。如何将arguemtns传递给addProduct方法?你能给我看看吗?看起来里面有错误。。如果一切正常,实体字段类型应该可以正常工作。另外,您说categoryId应该返回1、2、3或4,但您正在设置“property”=>“name”,可以吗?