Home » Php » php – How can I perform an inline editing inside a nested admin?

php – How can I perform an inline editing inside a nested admin?

Posted by: admin April 23, 2020 Leave a comment

Questions:

My problem is the following. I am using Sonata Admin with Symfony. In the Admin section, when I try to create an entity, nothing appears when I click on the add button (spelled “Ajouter”):

enter image description here

I get the following error: Call to a member function getName() on a non-object in the chrome console

Here’s how my entities hierarchy is, I have three objects that are linked together in the following way:

Video ---OneToOne--> String ---OneToMany--> LocalizedString

Simply, I have one video that will have a title and this title will be translated. Here are my entities:

LocalizedString

OSC\UtilsBundle\Entity\LocalizedString:
    type: entity
    table: null
    repositoryClass: OSC\UtilsBundle\Entity\LocalizedStringRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        locale:
            type: string
            length: '20'
        content:
            type: string
            length: 255

    manyToOne:
        parent:
            targetEntity: String
            mappedBy: localizedObjects


    lifecycleCallbacks: {  }

String

OSC\UtilsBundle\Entity\String:
    type: entity
    table: null
    repositoryClass: OSC\UtilsBundle\Entity\StringRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO

    oneToMany:
        localizedObjects:
            targetEntity: LocalizedString
            mappedBy: parent
            cascade: ["persist", "remove"]

    lifecycleCallbacks: {  }

Video

OSC\MySportBundle\Entity\Video:
    type: entity
    table: null
    repositoryClass: OSC\MySportBundle\Entity\VideoRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO

    oneToOne:
        title:
            targetEntity: OSC\UtilsBundle\Entity\String
            cascade: ["persist", "remove"]

    lifecycleCallbacks: {  }

So, I did this structure to facilitate the editing in SonataAdmin. If, through the admin dashboard, I want to edit a String, I can easily edit a string and translate it in many languages (this already works).

However, when I try to do that in the video admin, it seems that I cannot do inline editing (clicking the add button does not work) of the String object.

Here’s the relevant code in video admin class:

$formMapper
        ->add('title', 'sonata_type_admin', array('delete' => false, 'btn_add' =>false), array(
            'edit' => 'inline',
            'inline' => 'table',
        ));

From what I have found, it looks like two imbricated forms are not possible ? Is there a way to circumvent that restriction ? Or maybe it is my design that is not too good ?

Edit1: It looks like there is a patch coming on github: https://github.com/sonata-project/SonataAdminBundle/pull/1971#issuecomment-58023124

If anyone knows how I can use it I would appreciate.

How to&Answers:

In your code you are using delete which is not a valid option. Maybe you can try 'btn_delete' => false
Check the documentation for all the valid options here.

If this not working, maybe sonata_type_collection is the solution to your problem. Ensure that you are using the by_reference option the correct way depending on your relation.

Answer:

Try this in the form mapper :

  $formMapper 
         ->add('title', 'sonata_type_model_list', array(
                    'class' => 'YourBundle:String',
                    'required' => false,
                    'delete' => false, 
                    'btn_add' =>true,
                ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))
            ;

If the error persist try to get a look at the Doctrine2 documentation :
Doctrine2 One to One association mapping and then generate your entities

Answer:

You said that chrome console gives you error:

Call to a member function getName() on a non-object

So this error is not from javascript?

If error from PHP it means that when you try $object->getName() (it must by in OSC\UtilsBundle\Controller use Ctr+f “getName()” in file editor to find that line) $object is not an object, that can by because you maybe get obeject array, not single object. Try add var_dump($object); and you see what is it.