使用Symfony2&amp ;;将实体下拉列表保存到数据库中教义

时间:2014-11-21 10:01:37

标签: 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('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()));
    }
}

错误代码: 执行' INSERT INTO产品(id_seller,tag,hot,featured,new,shipping,id_category,status,date_update,updated_by,date_add)VALUES(?,?,?,?,?,?,? ,?,?,?,?)'与params [" 123"," 123",1,1,1,0,{}," 0"," 2014-11-21 10:59:06"," admin"," 2014-11-21 10:59:06"]:

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

非常感谢。

0 个答案:

没有答案