douze1332
2018-09-01 22:35
浏览 111
已采纳

Symfony - 通过表单将一个主要实体链接到第二个实体

I am currently using doctrine wrong as when I have a choiceType in my entity, I use this:

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

plus in my builder:

    ->add('type', ChoiceType::class, ['choices' => ['Pattern' => 0, 'Image' => 1]])

I would like now to have something cleaner and have another entity in my database that would be linked to this main entity. Here is what I did.

  1. Created a "category" entity (and created in DB):

     /**
     * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
     * @ORM\Table(name="vipbox_mep_category")
     */
     class Category {
         /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
         private $id;
    
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $category;
    
        /**
         * @return mixed
         */
        public function getCategory()
        {
            return $this->category;
        }
    
        /**
         * @param mixed $category
         */
        public function setCategory($category): void
        {
            $this->category = $category;
        }
    }
    
  2. linked my main entity to the second one (with the getter and setter):

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Category")
     */
    private $category;
    
  3. Added my category to my form:

    ->add('category', CategoryType::class, ['required' => true, 'label' => 'Category'])
    
  4. Extended AbstractType form for my category:

    class CategoryType extends AbstractType
    {
        /**
         * @var RouterInterface $route
         */
        private $router;
    
        /**
         * @param RouterInterface $router
         */
        public function __construct(RouterInterface $router)
        {
           $this->router = $router;
        }
    
        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'required' => true,
                'label' => 'Category'
            ]);
        }
    
        /**
         * {@inheritdoc}
        */
        public function getParent()
        {
            return ChoiceType::class;
        }
    }
    
  5. Populated my Category table with text sample data (2 rows)

  6. Tested to show my form, I does compute, but I have an empty choiceType:

enter image description here

I know I missed one/multiple things on my way, any clues ?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudou348131346 2018-09-01 22:45
    已采纳

    Assuming you have saved some data in your Category table to populate your drop-down list just change your step 3 from this:

    ->add('category', CategoryType::class, ['required' => true, 'label' => 'Category'])
    

    to this:

    ->add('category', EntityType::class, [
        'class' => Category::class,
        'required' => true,
        'label' => 'Category',
    ])
    

    There are lots of other options available for EntityType, see the docs.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题