简体   繁体   中英

Sonata admin “sonata_type_collection” tries to remove entity

I have two entities and two Admin classes(StripePage and Stripe) for them. Form looks good, it has my fields and button to add new subforms. When "add new" button is clicked, new subform with form from Admin class is rendered, but if you click second time on this button or try to save entity error appears:

Catchable Fatal Error: Argument 1 passed to Fred\\CoreBundle\\Entity\\StripePage::removeStripe() must be an instance of Fred\\CoreBundle\\Entity\\Stripe, null given in C:\\Users\\lubimov\\OpenServer\\domains\\tappic.dev\\src\\Fred\\CoreBundle\\Entity\\StripePage.php

So symfony tries to remove entity instead of adding it.

StripePageAdmin class:

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:

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

What is the problem? Stripe form in admin class must be configured by other way?

Well, solution that I use now is to set 'by_reference' => true in 'sonata_type_collection' settings.

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

After this need to declare setStripes() method in entity.

public function setStripes(\Doctrine\Common\Collections\ArrayCollection $stripes) {
    $this->stripes = $stripes;
}

If somebody finds how to solve this using addStripe() type of methods, please share it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM