【问题标题】:Argument 1 passed to Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() must be an object or null, string given传递给 Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\IdReader::getIdValue() 的参数 1 必须是对象或 null,给定字符串
【发布时间】:2022-11-04 18:58:58
【问题描述】:

博客类型

        ->add('category',EntityType::class,[
            'class' => Category::class,
            'choice_label' => function(Category $category) {
                return $category->getName();
            }
        ])

类别实体

/**
 * @ORM\Column(type="string", length=255)
 */
private $name;

public function __toString() {
    return $this->name;
}

public function getId(): ?int
{
    return $this->id;
}

public function getName(): ?string
{
    return $this->name;
}

控制器:

/**
 * @Route("/edit-post/{id}", name="edit-post")
 */
public function editBlogPost(ManagerRegistry $manager, $id){
    $post = $manager->getRepository(Blog::class)->find($id);
    $form = $this->createForm(BlogType::class, $post);
    $form->handleRequest($request);
    dd($post);
}

错误:

参数 1 传递给 Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() 必须是 一个对象或 null,给定的字符串

当我在 $post 中执行 dd 时,出现上述错误,有什么解决方案吗?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    我认为,由于您已经在实体 obj 中实现了 __toString(),因此您不需要在 BlogType 中定义choice_label。

    但是如果你删除__toString()(你应该让Symfony处理这个表格)你可以简单地写'choice_label' => 'name'

    【讨论】:

    • 你好@Giuseppe 我做了'choice_label' => 'name' 但仍然遇到同样的错误
    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多