sonata-project / SonataDoctrineORMAdminBundle

Integrate Doctrine ORM into the SonataAdminBundle
https://docs.sonata-project.org/projects/SonataDoctrineORMAdminBundle
MIT License
445 stars 344 forks source link

sonata_type_model_list: Impossible to invoke a method ("id") on a NULL variable ("") #252

Closed sandermarechal closed 4 years ago

sandermarechal commented 11 years ago

This may be related to #222.

I have a couple of entities with inheritance set up like so:

class My\Base

class My\Foo extends My\Base
    var $bar: Doctrine ORM many-to-one to My\Bar

class My\Bar

I have an Admin class that uses setSubClasses so I can manage all entities that extend Base using the same Admin class:

    <service id="sonata.admin.myBase" class="My\BaseAdmin">
        <argument />
        <argument>My\Base</argument>
        <argument>SonataAdminBundle:CRUD</argument>
        <tag name="sonata.admin" manager_type="orm" />
        <call method="setSubClasses">
            <argument type="collection">
                <argument key="myFoo">My\Foo</argument>
                <argument key="myQuu">My\Quu</argument>
                <!-- ... -->
            </argument>
        </call>
    </service>

In my BaseAdmin I generate a form like this:

    protected function configureFormFields(FormMapper $formMapper)
    {
        // Add fields from My\Base
        // ...

        // Add fields from My\Foo
        if ($this->getSubject() instanceof MyFoo) {
            $formMapper->add('bar', 'sonata_type_model_list');
        }
    }

Now, when I create or edit an entity I get the following error:

Impossible to invoke a method ("id") on a NULL variable ("")

If I use a sonata_type_model instead of a sonata_type_model_list then it works. Also, if I move the bar property from My\Foo to My\Base it also works correctly. The error only appears when I use a model_list type for a relation that only exists in a subclass.

stof commented 11 years ago

A ManyToOne relation means that the bar property contains a model, not a list of models

sandermarechal commented 11 years ago

That is what sonata_type_mode_list does, right?. It gives you a paginatable list so you can choose one instance from it. I've started using it because some of my tables are too big to use with a sonata_type_model, which tries to load everything in order to display a select box.

Please let me know if I have this backwards somehow.

pmartelletti commented 11 years ago

I'm having the same issue here... have you found any workaround?

johannes85 commented 11 years ago

You have to generate an admin class for the entity you want to choose.

nrodriguez-tupaca commented 8 years ago

3 years later, I have the same issue here! in reply to johannes85, my "Bar" entity is Media (from sonata-media-bundle), so it already has an admin class.

Did anyone solve this?

soullivaneuh commented 8 years ago

3 years later, I have the same issue here!

Being sarcastic would not help at all.

Do you get this error with the 3.x version of this bundle? 2.x is not maintained anymore.

gvizquel commented 7 years ago

Hello everyone. At this point I'm having the same problem using symfony 3 and sonata admin-bundle 3. Could anyone fix this? Thank you

quentinboudet commented 5 years ago

Hi everyone, if it can help I had the same bug on symfony4 and resolved it simply by defining the 'admin_code' like this :

$formMapper->add('lastFormResponse', ModelListType::class, [
    'class' => FormResponse::class,
], [
    'admin_code' => 'admin.form_response',
]);

So it seems the Admin guesser of ModelListType doesn't work with complex relations, the only difference in my code is that 'lastFormResponse' get the last occurrence in an ArrayCollection.

ModelType maybe suffer the same bug, because the select work but the add button disappear, but not sure because 'admin_code' doesn't work this time.

abouross commented 5 years ago

if your target entity is "Media" then do not forget to specify "db_driver: doctrine_orm" in the configuration

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.