2

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.

4

1 回答 1

0

看来,您正在尝试手动创建/更新 M:N 关系的记录。这不是必需的,因为 Dotrine ORM 会自动在 M:N 关系的中间表中创建或删除这些记录。

  • 检查您的数据库结构中是否有正确的关系。您可以在一些数据库建模应用程序中可视化您的数据库结构,例如Mysql Workbench

  • 将您的数据库结构正确映射到学说配置文件。对您的数据库进行逆向工程可能会有所帮助: http : //symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html 在原则配置文件中“User.orm.xml”应该是多对多关系。例如:

    <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> 
  • 将 __toString() 方法添加到您的 City 实体:

    //...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.
    }
    
  • 在 AcmeExampleBundle/Admin/UserAdmin.php 文件中:

    $formMapper
            ->add('cityId', 'sonata_type_model', array(
                'required' => false,
                'label'    => $this->trans('City name'),
                'expanded' => true,
                ));
    
于 2012-10-07T17:35:29.360 回答