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.
-
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; } }
-
linked my main entity to the second one (with the getter and setter):
/** * @ORM\OneToOne(targetEntity="App\Entity\Category") */ private $category;
-
Added my category to my form:
->add('category', CategoryType::class, ['required' => true, 'label' => 'Category'])
-
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; } }
Populated my Category table with text sample data (2 rows)
- Tested to show my form, I does compute, but I have an empty choiceType:
I know I missed one/multiple things on my way, any clues ?