31
votes

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.

3
Simply use 'sonata_type_model' and pass it's related options to get better controlsLost Koder
Have you tried 'inline' => 'standard' with sonata_type_collection instead of sonata_type_admin with 'inline'=>'table' ?Grzegorz Krauze
And also what is your StringAdmin ?Grzegorz Krauze

3 Answers

1
votes

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.

0
votes

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

-3
votes

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.