Sonata 管理员"sonata_type_collection"尝试删除实体



我有两个实体和两个admin类(脱衣舞和条纹)。表格看起来不错,它具有我的字段和按钮可以添加新的子形式。单击"添加新"按钮时,渲染了带有Admin类表单的新子形式,但是如果您在此按钮上单击第二次或尝试保存实体错误:

可捕获的致命错误:参数1传递给 Fred CoreBundle Entity stripepage :: removEstripe()必须是一个实例 Fred coreBundle Entity Stripe,null c: users lubimov openServer domains tappic.dev src fred corebundle entity entity stripepage.php

因此,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();
    }
}

straineadmin类:

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();
    }
}

有什么问题?管理类中的条纹形式必须以其他方式配置?

好吧,我现在使用的解决方案是在'sonata_type_collection'设置中设置 'by_reference' => true

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(DoctrineCommonCollectionsArrayCollection $stripes) {
    $this->stripes = $stripes;
}

如果有人找到了使用addStripe()类型的方法来解决此问题,请在此处分享。

最新更新