如何修复对象无法转换为字符串错误

时间:2019-03-26 15:20:21

标签: orm twig symfony4

我想在表格上进行研究,提交数据时会在搜索表单中显示此错误:

  

可捕获的致命错误:App \ Entity \ DAORecherche类的对象无法转换为字符串

$daoRecherche= new DAORecherche();
$form = $this->createForm(DAORechercheType::class, $daoRecherche);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();

    $RechDao = $repo->find($data);
    return $this->render('dao/chercher_dao.html.twig', [
        'marche' => $RechDao
    ]);
}

return $this->render('dao/liste_dao.html.twig', [
    'daos'=> $daos,
    'form' => $form->createView()
]);

1 个答案:

答案 0 :(得分:0)

该错误可能是因为您尚未在实体类__toString中定义DAORecherche方法。

编辑类DAORecherche并添加类似以下代码的内容:

public function __toString(): string
{
    return $this->foo; // where foo is a property of DAORecherche
    // or alternatively
    return sprintf('% - %', $this->foo, $this->bar); // where foo and bar are properties of DAORecherche
}

该方法必须返回一个字符串,您可以根据需要定义逻辑。该代码示例只是指出一些选项。

Official documentation