2
votes

I am new to sonata admin bundle

I have created a form in sonata admin bundle.

 protected function configureFormFields(FormMapper $formMapper)
   {

    $formMapper
    ->with('General')

    ->add('userid', null, array('label' => 'User'))
    ->add('cityid', null, array('label' => 'City Name'))

    ->end();    
   }

Here userid and cityid are composite key.

I am able to create a new record successfully. But Updation on the same record by changing any one of the composite key creates a problem.

The record is updated successfully in the database but it throws the exception

unable to find the object with id : 1~1 

where 1~1 is the id of user and city prior its updation. How do i resolve this Exception?

Thanks in advance.

1

1 Answers

0
votes

It seems, that you are trying to manually create/update records for M:N relation. This isn't needed, because Dotrine ORM will create or delete these records in middle table for M:N relation automatically.

  • Check, that you have correct relations in your database structure. You can visualize your database structure in some database modeling application, like Mysql Workbench.

  • Map correctly your database structure to doctrine config files. Reverse engineering of your database may help: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html In doctrine config file "User.orm.xml" should be many-to-many relation. For example:

    <many-to-many field="cityId" target-entity="City" inversed-by="userId">
      <join-table name="user_has_city">
        <join-columns>
          <join-column name="id_user" referenced-column-name="id_user"/>
        </join-columns>
        <inverse-join-columns>
          <join-column name="city_id" referenced-column-name="city_id"/>
        </inverse-join-columns>
      </join-table>
    </many-to-many> 
  • Add __toString() method to your City entity:

    //...some code of entity...
    
    public function __toString()
    {
        return $this->name; //or $this->title, $this->label etc. - based on the name of variable, which stores the city's name.
    }
    
  • In file AcmeExampleBundle/Admin/UserAdmin.php:

    $formMapper
            ->add('cityId', 'sonata_type_model', array(
                'required' => false,
                'label'    => $this->trans('City name'),
                'expanded' => true,
                ));