我有两个实体和两个管理类(StripePage和Stripe)。表单看起来不错,它有我的字段和按钮来添加新的子窗体。单击“添加新”按钮时,呈现带有“Admin类的窗体”的新子窗体,但如果您第二次单击此按钮或尝试保存实体错误,则会出现以下错误:
可捕获的致命错误:传递给Fred\CoreBundle\Entity\StripePage::removeStripe()的参数1必须是\Entity\Stripe的实例,在C:\Users\lubimov\OpenServer\domains\tappic.dev\src\Fred\CoreBundle\Entity\StripePage.php中给出null
因此symfony尝试删除实体而不是添加它。
StripePageAdmin类:
class StripePageAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id', null, array('label' => 'id'))
->add('name', null, array('label' => 'Page name'));
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array('label' => 'Page name'))
->add('stripes', 'sonata_type_collection',
array('label' => 'Stripes', 'required' => false, 'by_reference' => false),
array('edit' => 'inline', 'inline' => 'table', 'sortable' => 'pos')
)
->end();
}
}StripeAdmin类:
class StripeAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('picture', null, array('sortable' => true))
->add('text', null, array('sortable' => true))
->add('deep_link', null, array('sortable' => true));
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('file', 'file', array('label' => 'Picture'))
->add('text', null, array('label' => 'Text'))
->add('deep_link', 'choice', array(
'choices' => array('test1' => 'Test1', 'test' => 'Test2'),
'label' => 'Deep Link',
))
->end();
}
}有什么问题吗?管理类中的条带表单必须以其他方式配置吗?
发布于 2015-05-12 06:51:25
我现在使用的解决方案是在“'by_reference' => true”设置中设置sonata_type_collection。
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array('label' => 'Page name'))
->add('stripes', 'sonata_type_collection',
array('label' => 'Stripes', 'required' => false, 'by_reference' => true),
array('edit' => 'inline', 'inline' => 'table', 'sortable' => 'pos')
)
->end();
}之后,需要在实体中声明setStripes()方法。
public function setStripes(\Doctrine\Common\Collections\ArrayCollection $stripes) {
$this->stripes = $stripes;
}如果有人发现如何使用addStripe()类型的方法解决这个问题,请在这里分享。
https://stackoverflow.com/questions/30181591
复制相似问题