Symfony 4 - choice_value FormType不起作用

时间:2018-05-24 20:21:21

标签: forms symfony request symfony4

这是我控制器中的方法:

public function parentCat($parentId,$manOrWomen,Request $request){


        $pr=new Product();
        $products=$this->getDoctrine()->getRepository(Product::class)->listProductsParentCategory($parentId,$manOrWomen);
        $form=$this->createForm(SearchProductType::class,$pr,['parentId'=>$parentId,'manOrWomen'=>$manOrWomen]);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()){

            $price=$request->request->get('search_product','price');
            $name=$request->request->get('search_product','name');
            $products=$this->getDoctrine()->getRepository(Product::class)->listSearchedProd($parentId,$manOrWomen,$price['price']
            ,$name['name']);

            //$products=$this->getDoctrine()->getRepository(Product::class)->listSearchedProd($productPrice);
            return $this->render('list_products/index.html.twig',['controller_name' => 'ListProductsController',
                'form1'=>$form->createView(),'products'=>$products]);

        }
        else{
            return $this->render('list_products/index.html.twig',['controller_name' => 'ListProductsController','form1'=>$form->createView()
            ,'products'=>$products]);
        }


    }

这是我的产品类(Pruduct Entity):

class Product
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     *
     * @ORM\Column(type="string", length=255)
     * @Assert\Image(mimeTypes={"image/jpeg","image/png"})
     */
    private $image;

    /**
     * @ORM\Column(type="integer")
     */
    private $price;

    /**
     * @var Category[] | ArrayCollection
     * @ORM\ManyToOne(targetEntity="App\Entity\Category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;



    /**
     * @var ManOrWomen[] | ArrayCollection
     * @ORM\ManyToOne(targetEntity="App\Entity\ManOrWomen")
     * @ORM\JoinColumn(name="manorwomen_id", referencedColumnName="id")
     */
    private $manorwomen;

    /**
     * @param \DateTime $createdDate
     *
     * @ORM\Column(name="createdDate", type="datetime")
     */
    private $createdDate;
    /**
     * Product constructor.
     */
    public function __construct()
    {
        $this->createdDate=new \DateTime('now');
        $this->category = new ArrayCollection();
        $this->manorwomen=new ArrayCollection();
        $this->undercategory=new ArrayCollection();
        $this->parentCategory=new ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getPrice()
    {
        return $this->price;
    }

    public function setPrice($price): self
    {
        $this->price = $price;

        return $this;
    }

    /**
     * @return Category[]|ArrayCollection
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param Category[]|ArrayCollection $category
     */
    public function setCategory($category): void
    {
        $this->category = $category;
    }

    /**
     * @return ManOrWomen[]|ArrayCollection
     */
    public function getManorwomen()
    {
        return $this->manorwomen;
    }

    /**
     * @param ManOrWomen[]|ArrayCollection $manorwomen
     */
    public function setManorwomen($manorwomen): void
    {
        $this->manorwomen = $manorwomen;
    }

    /**
     * @return DateTime
     */
    public function getCreatedDate(): DateTime
    {
        return $this->createdDate;
    }

    /**
     * @param DateTime $createdDate
     */
    public function setCreatedDate(DateTime $createdDate): void
    {
        $this->createdDate = $createdDate;
    }

这是我的产品类型的FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $parentId = $options['parentId'];
        $manOrWomen=$options['manOrWomen'];
        $builder
            ->add('price',EntityType::class,[
                'class'=>Product::class,
                'choice_label'=>'price',
                'choice_value' => 'price',
                'placeholder'=>'Default',
                'query_builder' => function (EntityRepository $er) use ($parentId,$manOrWomen) {
                    return $er->createQueryBuilder('product')
                        ->innerJoin('product.category','c')
                        ->addSelect('c')
                        ->innerJoin('product.manorwomen','m')
                        ->addSelect('m')
                        ->where('c.parent_id=:parentId')
                        ->andWhere('m.id=:manOrWomen')
                        ->setParameters(['parentId'=>$parentId,'manOrWomen'=>$manOrWomen]);


                },
                'expanded'=>false,
                'multiple'=>false
            ])
            ->add('name',EntityType::class,[
                'class'=>Product::class,
                'choice_label'=>'name',
                'choice_value' => 'name',
                'placeholder'=>'Default',
                'query_builder' => function (EntityRepository $er) use ($parentId,$manOrWomen) {
                    return $er->createQueryBuilder('product')
                        ->innerJoin('product.category','c')
                        ->addSelect('c')
                        ->innerJoin('product.manorwomen','m')
                        ->addSelect('m')
                        ->where('c.parent_id=:parentId')
                        ->andWhere('m.id=:manOrWomen')
                        ->setParameters(['parentId'=>$parentId,'manOrWomen'=>$manOrWomen]);


                },
                'expanded'=>false,
                'multiple'=>false
            ])
            ->add('submit',SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Product::class
        ]);
        $resolver->setRequired(
            ['parentId','manOrWomen']
        );
    }

我对请求有疑问:

enter image description here

“price”=> “44”(这是当前商品的价格。这没关系。)         “name”=> “44”(这必须是当前项目的名称。)

我有'choice_value'=>'名称',但我得到价格而不是名字。

如果我用另一个值改变第一个choice_value,则第二个choice_value变得等于她。为什么?

当我从第一个EntityType删除addSelect工作正常,但如果我添加更多一个EntityType,那么我有同样的问题 - thirth EntityType值等于第二个值('name')。

1 个答案:

答案 0 :(得分:0)

我会说你的实体在名称的getter中返回价格,或者价格的设定者设置名称的值。