简体   繁体   English

在sonata:admin:generate之后如何更改ID字段的行为

[英]How to change ID field behavior after sonata:admin:generate

I have a fresh admin controller that extends Sonata's CRUDController class. 我有一个新的管理员控制器,它扩展了Sonata的CRUDController类。 I generated it using the sonata:admin:generate command. 我使用sonata:admin:generate命令生成了它。

There is also a new PostAdmin.php file that defines several field behaviors. 还有一个新的PostAdmin.php文件,它定义了几个字段行为。

Here is a look at the table used by the entity: 这是实体使用的表:

mysql> DESC post;
+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| title  | varchar(255) | NO   |     | NULL    |                |
| author | varchar(255) | NO   |     | NULL    |                |
| slug   | varchar(255) | NO   |     | NULL    |                |
| body   | longtext     | NO   |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

My question is: How do I go about making the ID field auto-populate in my CMS? 我的问题是: 如何使CMS中的ID字段自动填充?

Right now, it shows up as a normal int field that users have to fill out, which introduces lots of potential for skipped numbers (not to mention being annoying for the users). 现在,它显示为用户必须填写的普通int字段,这为跳过数字带来了很多潜在的可能性(更不用说让用户感到烦恼了)。 I would like to change that. 我想改变这一点。

This can be achieve this by configuring the Form Mapper of your PostAdmin class: 这可以通过配置 PostAdmin的Form Mapper来实现:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('title', 'text')
        ->add('body', 'textarea')
    ;
}

You would need to remove the ->add('id') line so that it is not part of the form. 您需要删除->add('id')行,使其不属于表单。 Because it isn't necessary for the creation and should not be edited anyway. 因为它对于创建不是必需的,并且无论如何都不应对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM