sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

"Add" from within forms on admins with subClasses wont work as expected #2876

Closed digitalkaoz closed 9 years ago

digitalkaoz commented 9 years ago

if a relation points to an entity with a discriminator map, the "Add" Button doesnt work, because it tries to create the abstract parent class:

/**
 * Class AbstractFilter
 *
 * @ORM\Entity()
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *  "value" = "Dl\Component\DomainModel\Filter\Condition\ValueFilter"
 * })
 * 
 */
abstract class AbstractFilter implements Filter
{
}

/**
 * Class ValueFilter
 *
 * @ORM\Entity()
 */
class ValueFilter extends AbstractFilter
{
}

i have admins for both and linked the childs into the parent:

        <service id="dl_filter.admin.abstract_filter" class="Dl\Bundle\FilterBundle\Admin\AbstractFilterAdmin">
            <tag name="sonata.admin" manager_type="orm" group="admin" label="AbstractFilter"/>
            <argument>null</argument>
            <argument>Dl\Component\DomainModel\Filter\Condition\AbstractFilter</argument>
            <argument>SonataAdminBundle:CRUD</argument>
            <call method="setSubClasses">
                <argument type="collection">
                    <argument key="value">Dl\Component\DomainModel\Filter\Condition\ValueFilter</argument>
                </argument>
            </call>
            <call method="addChild"><argument type="service" id="dl_filter.admin.value_filter" /></call>
        </service>

        <service id="dl_filter.admin.value_filter" class="Dl\Bundle\FilterBundle\Admin\ValueFilterAdmin">
            <tag name="sonata.admin" manager_type="orm" group="admin" label="ValueFilter"/>
            <argument>null</argument>
            <argument>Dl\Component\DomainModel\Filter\Condition\ValueFilter</argument>
            <argument>SonataAdminBundle:CRUD</argument>
        </service>

when using the from the "List View" the Menu Drop Down behaves correct, and gives me a "Add ValueFilter" Button to create a new Value Filter.

But when using it from within a form like this:

//the admin
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('must', 'sonata_type_collection', ['label' => 'Must'], ['inline' => 'table', 'edit' => 'inline']) 
            //points to "dl_filter.admin.abstract_filter"
        ;
    }

i get an add button, but that wont work as the parent class is abstract...best would be the same dropdown like in the list view, or multiple buttons for each subClass.

Maybe im doing it wrong? Any Help?

sfavot commented 9 years ago

Same problem here. I was able to generate different add buttons for each subclasses by overriding the edit_orm_one_to_many template but of course it's not enough since the controller doesn't handle this case. The problem only occurs if you want to be able to add any type of children in your collection. If you only need to add ValueFilter objects, you can fix the problem by specifying 'type_options' => ['data_class' => '\Dl\Component\DomainModel\Filter\Condition\ValueFilter'] when creating your form.

rande commented 9 years ago

Should be fixed by https://github.com/sonata-project/SonataDoctrineORMAdminBundle/commit/501e0dcec17f8d847623b3c73310f258c052e5e2

Tofandel commented 7 years ago

Nope this doesn't fix it.

OskarStark commented 7 years ago

@Tofandel can you elaborate or do you have a fix which you could provide in a PR?

Tofandel commented 7 years ago

Well, there is still the same Add button in inline (so no dropdown) and so it's still throwing the same error: Cannot initialize abstract class. I tried a lot of things and I can't find a way to get the dropdown, so the only thing that does work is displaying it in a modal.

I also have an issue with my sonata_entity_collection, once it has childrens, it's not displayed correctly on the page : image

I would expect a button to open a modal to edit the Children, but instead I just get all of the fields within the parent form and it's all a big mess. Not really related, but do you happen to know where could I be doing something wrong ? Maybe some twig form_theme config I'm missing, but I can't find it documented, thanks.