首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sonata "sonata_type_collection“尝试删除实体

Sonata "sonata_type_collection“尝试删除实体
EN

Stack Overflow用户
提问于 2015-05-12 04:04:43
回答 1查看 1.1K关注 0票数 2

我有两个实体和两个管理类(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类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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();
    }
}

有什么问题吗?管理类中的条带表单必须以其他方式配置吗?

EN

回答 1

Stack Overflow用户

发布于 2015-05-12 06:51:25

我现在使用的解决方案是在“'by_reference' => true”设置中设置sonata_type_collection。

代码语言:javascript
复制
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()方法。

代码语言:javascript
复制
public function setStripes(\Doctrine\Common\Collections\ArrayCollection $stripes) {
    $this->stripes = $stripes;
}

如果有人发现如何使用addStripe()类型的方法解决这个问题,请在这里分享。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30181591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档