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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题