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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同