Doctrine Schema 更改为 id 自动递增

发布于 2024-09-24 14:27:25 字数 605 浏览 14 评论 0原文

我正在使用 YAML 定义学说模式,并希望使用 0 以外的数字(例如 324)来启动在 auto-increment 上设置的 id 字段。在 mysql 中通过执行类似 AUTO_INCRMENT=324 的操作来完成

Google 群组线程 有一个提示,可能可以使用 command.pre_command 事件在加载固定装置之前执行 SQL 和引用symfony 文档中的此页面< /strong>,但提示和引用的页面实际上都没有解释已知的方法,有谁知道如何实际执行此操作?

I'm using YAML to define the doctrine schema and would like to start the id field that's set on auto-increment with a number other than 0, let's say 324 (this is done in mysql by doing something like AUTO_INCREMENT=324.

This Google groups thread has a hint that it may be possible to do with command.pre_command event to execute the SQL before fixtures are loaded and references this page from the symfony documentation, but both the hint and the referenced page don't actually explain a known way to do it. Does anyone know how to actually do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-10-01 14:27:25

看来 Doctrine 1 并不支持开箱即用,因此人们告诉您挂钩 Symfony CLI 任务系统在启动新任务(加载灯具)时生成的事件。这可行,但我不知道这是否可以从 YAML 值加载值(也许常规的“表创建任务”会因为额外的参数而阻塞?)。最简洁的方法是编写执行 Doctrine 内容的任务的(小)子类,它理解您传递给 YAML 文件的额外参数。这将首先定期创建数据库,然后设置自动增量基值。

一种更简单的方法(实现,也许不执行)是创建一个包含表名称和基值的单独文件,该文件由 您编写的新 Symfony 任务,它创建并执行必要的查询。只是不要忘记在创建数据库之后但加载灯具之前执行此任务。并在每次添加表时更新配置文件。

最快速但最肮脏的方法是使用一个包含所有查询的额外 SQL 脚本,以及一个连续执行所有内容的 shell 脚本。但这需要您进行最多的维护。

It appears Doctrine 1 does not support this out of the box, so people are telling you to hook into the event that the Symfony CLI task system generates when it starts a new task (the loading of the fixtures). This would work, but I don't know whether this can load the value from the YAML value (maybe the regular "table creation task" would choke on the extra parameter?). The cleanest way to do this is to write a (little) subclass of the task that executes the Doctrine stuff, which understands an extra parameter you pass to the YAML file. This would first do the regular creation of the databases, and then set the autoincrement base values.

An even easier way (to implement, maybe not to execute) would be to create a separate file with the table names and base values, which is read by a new Symfony task you write, which creates and executes the necessary queries. Just don't forget to execute this task after you create the database but before you load the fixtures. And to update the configuration file every time you add a table.

The most quick-and-dirty way would be an extra SQL script with all the queries, and a shell script that executes everything in a row. But this takes the most maintenance on your side.

挽清梦 2024-10-01 14:27:25

您可以尝试在数据库中手动设置自动增量,然后转储架构文件并查看它在 YML 中为您设置的自动增量输出的内容。

转储模式的命令是 symfonydoctrine::build-schema

它将在 config/doctrine 目录中创建 YML 文件。请记住,它将覆盖当前位于该目录中的架构文件,因此您需要在尝试执行此操作之前对其进行备份。

You could try to set the AutoIncrement manually in the database, THEN dump the Schema file and see what it outputs in the YML for the AutoIncrement you set.

The command to dump the schema is symfony doctrine::build-schema

It will create the YML file in the config/doctrine directory. Keep in mind that it will overwrite the schema file that is currently in that directory, so you will want to back that up before you attempt doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文