1

I'm displaying sub categories but getting following error:

Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue(): Argument #1 ($object) must be of type ?object, string given, called in C:\xampp\htdocs\demo_app\vendor\symfony\form\ChoiceList\ArrayChoiceList.php on line 181

    $formModifier = function(FormInterface $form, Category $category = null){
       
        $subCategories = null === $category ? [] : $category->getSubCategories();
        $subCategoryChoices = [];
        foreach ($subCategories as $subCategory) {
            $subCategoryChoices[$subCategory->getId()] = $subCategory->getName();
        }
        $form->add('sub_category',EntityType::class,[
            'class' => SubCategory::class,
            'choices' => $subCategoryChoices,
        ]);
    };

As per the documentation if I do the following:

    $formModifier = function(FormInterface $form, Category $category = null){
       
        $subCategories = null === $category ? [] : $category->getSubCategories();
        $form->add('sub_category',EntityType::class,[
            'class' => SubCategory::class,
            'choices' => $subCategories ,
        ]);
    };

I get following error:

Object of class App\Entity\SubCategory could not be converted to string
Demi God
  • 87
  • 9

1 Answers1

1

So I solved the issue using the following code.

  public function __toString()
    {
        return $this->name;
    }
Demi God
  • 87
  • 9