我使用命令向我的项目( BookAuthor、BookSeries )添加了 2 个实体php bin/console make:entity
,然后使用make:form
命令生成了一个表单类。
在测试时,我添加了一些作者,但是当我尝试打开路线时,add/Series
我收到此错误消息:
App\Entity\BookAuthor 类的对象无法转换为字符串
Form Builder 类的相关代码:
class BookSeriesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('SeriesAuthor', )
->add('Submit', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => BookSeries::class,
]);
}
}
SeriesAuthor应该从数据库中获取可用作者的列表。
BookSeries 与 BookAuthor 实体具有 OneToMany 关系。虽然 BookAuthor 表为空,但添加作者后页面加载正常,但页面开始出现错误
Controller.php 文件中的相关代码:
public function addBookSeries(Request $request)
{
$new_series = new BookSeries();
$form = $this->createForm(BookSeriesType::class, $new_series);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($new_series);
$em->flush();
return $this->redirect($this->generateUrl('post.manage'));
}
return $this->render("post/create.html.twig",[
'form' => $form->createView()
]);
白猪掌柜的
慕容森