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.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备