在表单中嵌入单个对象

时间:2016-01-27 10:11:06

标签: php symfony orm doctrine

我设法创建一个嵌入另一种形式的表单,但我认为我没有做正确的事情。这是我的代码

分类

   class Category
    {
        private $id;

        private $name;

        /**
         * @ORM\OneToMany(targetEntity="Category", mappedBy="category")
         */
        private $subcategorues;

        public function __construct()
        {
            $this->subcategorues = new \Doctrine\Common\Collections\ArrayCollection();
        }


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

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

            return $this;
        }

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


        public function addSubcategorue(\AppBundle\Entity\Category $subcategorues)
        {
            $this->subcategorues[] = $subcategorues;

            return $this;
        }

        public function removeSubcategorue(\AppBundle\Entity\Category $subcategorues)
        {
            $this->subcategorues->removeElement($subcategorues);
        }

        public function getSubcategorues()
        {
            return $this->subcategorues;
        }

    }

子类别

class Subcategory
{
    private $id;

    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="subcategories")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;

    /**
     * @return mixed
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param mixed $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


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

        return $this;
    }

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

CategoryType

.......
 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'entity', [
                'class' => 'AppBundle\Entity\Category',
                'choice_label' => 'name'
            ]);
    }

 public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => 'AppBundle\Entity\Category'
    ]);
}
......

SubcategoryType

 $builder
            ->add('category', new CategoryType(), [
                'label' => false
            ])
            ->add('name', 'text')
            ->add('save', 'submit')
            ;    


  public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => 'AppBundle\Entity\Subcategory'
    ]);
}

DefaultController

public function indexAction(Request $request)
    {

        $subcategory = new Subcategory();

        $form = $this->createForm(new SubcategoryType(), $subcategory);

        $form->handleRequest($request);

        if($form->isValid()){

            $em = $this->getDoctrine()->getManager();
            $subcategory->setCategory($subcategory->getCategory()->getName());

            $em->persist($subcategory);
            $em->flush();

            return new Response(sprintf('ID %d', $subcategory->getId()));
        }

        return $this->render('AppBundle::layout.html.twig', [
          'form' => $form->createView(),
        ]);
    }

请注意这行代码$subcategory->setCategory($subcategory->getCategory()->getName()); 我需要该行以将实体保存到数据库,否则我会收到错误。所以我的问题是有没有办法跳过这行代码并将类别对象动态传递给subcategory-> category属性而不是手动执行?

// EDIT

这里是dump的输出($ form-> getData());

DefaultController.php on line 33:
Subcategory {#467 ▼
  -id: null
  -name: "Uncharted"
  -category: Category {#588 ▼
    -id: null
    -name: Category {#685 ▼
      -id: 2
      -name: "Games"
      -subcategorues: PersistentCollection {#686 ▶}
    }
    -subcategorues: ArrayCollection {#660 ▶}
  }
}

2 个答案:

答案 0 :(得分:1)

与您的CategoryType实体相比,您的Category未正确映射。实际上,在您的情况下,您不需要使用CategoryType字段name,因为您在category中有一个SubCategory字段,是Category的关系。

只需替换:

->add('category', new CategoryType(), [
            'label' => false
        ])

由:

->add('category', 'entity', [
            'class' => 'AppBundle\Entity\Category',
            'choice_label' => 'name'
        ]);

答案 1 :(得分:0)

你的尝试可以像这样(对于Category实体类):

public function addSubcategorue(\AppBundle\Entity\Category $subcategorues)
{
    if ($this->subcategorues->contains($subcategorues)) {
        $this->subcategorues->add($subcategorues);
        $subcategorues->setCategory($this);
    }

    return $this;
}