试图生成一个表单,其中它将是内容的集合,但不幸的是,她失去了错误,不知道如何恢复。
给我错误
注意:未定义的索引:pageId
500内部服务器错误-ContextErrorException
页面实体:
class Page
{
private $id;
private $name;
/**
* @ORM'OneToMany(targetEntity = "PageContent", mappedBy = "pageId")
*/
private $content;
}
PageContent实体:
class PageContent
{
private $id;
/**
* @ORM'ManyToOne(targetEntity = "Page", inversedBy = "page_content")
* @ORM'JoinColumn(name = "page_id", referencedColumnName = "id", onDelete = "SET NULL")
*/
private $page;
private $name;
}
编辑页面内容类型:
<?php
namespace Eteam'SettingsBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolver;
class EditPageContentsType extends AbstractType
{
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'EditPageContents';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('pageContentMap')
->add('content', 'collection', array(
'type' => new PageContentType(),
'options' => array(
'required' => false
),
'allow_add' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Eteam'PageBundle'Entity'Page',
));
}
}
页面内容类型:
<?php
namespace Eteam'SettingsBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolver;
class PageContentType extends AbstractType
{
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'pageContent';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Test'
))
->add('content')
->add('type')
->add('pageId');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Eteam'PageBundle'Entity'PageContent',
));
}
}
我将感谢你的帮助。
正因为如此,我在这里和Synfony 2中都是初学者,请不要点击否定。谢谢
p.S.
如果你需要更多信息,请告诉我。
首先,您的注释是错误的。
页面应为:
* @ORM'OneToMany(targetEntity = "PageContent", mappedBy = "page")
PageContent应为:
* @ORM'ManyToOne(targetEntity = "Page", inversedBy = "content")
mappedBy和invertedBy与关系的对象的属性名直接相关,并且必须相同。
其次,由于PageContent是Page的子级,您不需要在PageContentType中使用pageId。这是一种更好的做事方式。
在EditPageContentsType中,添加选项'by_reference' => false
请参阅此处了解的原因
然后更改页面中的addContent()方法
public function addContent(PageContent $pageContent)
{
$pageContent->setPage($this);
$this->content->add($pageContent);
}
类似于本文档中的addTag示例
这使您可以轻松地将页面与PageContent实体关联起来。我建议你好好阅读一下表格文档,因为你会经常看到这种东西。