向 Symfony 中的现有实体添加一列

2023-08-19php开发问题
0

本文介绍了向 Symfony 中的现有实体添加一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在我的 web 服务器上玩弄 Symfony,我一直在为我的数据库创建带有学说的实体.我想向这些实体之一添加一列......我想做这样的事情:

I've been playing around with Symfony on my web server and I've been creating entity with doctrine for my database. I wanted to add a column to one of these entity... I wanted to do something like:

php app/console doctrine:modify:entity

现在我知道这个命令不存在,但是有没有一种方法(无需进行整个迁移)来简单地添加一列.

Now I know that this command doesn't exists, but is there a way (without doing a whole migration) to simply add a column.

附言我知道我可以打开 php 文件并在那里以文本方式添加列,然后更新架构,但我将其分发给一些客户端,我喜欢更类似命令行"的方法.

P.S. I know I could open the php file and textually add the column there and then update the schema, but I'm distributing this to some clients and I like a more "command-line-like" approach.

推荐答案

实际上,使用 Doctrine 来做你建议的事情根本没有意义.

Actually, using Doctrine does not make sense at all to do something like you suggested.

Doctrine 是一个 ORM(对象关系映射)工具.这意味着您想从 PHP 代码中抽象出数据库,将数据库的内容委托给 Doctrine.Doctrine 在这方面做得很好.

Doctrine is a ORM (Object-Relational Mapping) tool. It means that you want to abstract the database from your PHP code, you delegate database stuff to Doctrine. Doctrine does a wonderful job on that area.

因为你想让你的客户/同行更新模型的最新版本,你应该使用 Doctrine Migrations ( http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html).这就是管理数据库更新的方式.此外,它使您可以完全控制升级/降级数据库时要执行的操作.例如,您可以在添加 FK 之前设置默认值.

As you want to keep your customers/peers updated with the latest version of the model, you should use the Doctrine Migrations ( http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html ). That's the way to manage database updates. Moreover, it gives you complete control on what to do when upgrading/downgrading the database. You could, e.g., set default values before you add the FK.

在类上添加新属性的步骤应该是:

The steps for adding a new property on the class should be:

对于 Symfony 2:

  • 通过以下方式修改类:

  • modify the class by:

  • AcmeMyBundleEntityCustomer 并添加你想要的属性;

  • AcmeMyBundleEntityCustomer and add the property you want;

AcmeMyBundleEntityCustomer

或修改学说文件:

Acme/MyBundle/Resources/config/doctrine/customer.yml

运行控制台命令(它将在类中添加正确的设置/获取)

run the console command (it will add the proper set/get in the class)

php 应用/控制台学说:生成:实体 AcmeMyBundle:客户

运行控制台命令

php 应用程序/控制台原则:迁移:差异

运行控制台命令(它将在 app/DoctrineMigrations 上放置一个新文件)

run the console command (it will place a new file on app/DoctrineMigrations)

php 应用程序/控制台准则:迁移:迁移

在部署新版本的代码时,您所要做的就是更新源代码并运行上面的命令.

When you're deploying the new version of the code, all you got to do is update the source code and run the command above.

对于 Symfony 3:

  • 通过以下方式修改类:

  • modify the class by:

  • AcmeMyBundleEntityCustomer 并添加你想要的属性;

  • AcmeMyBundleEntityCustomer and add the property you want;

AcmeMyBundleEntityCustomer

或修改学说文件:

Acme/MyBundle/Resources/config/doctrine/customer.yml

运行控制台命令(它将在类中添加正确的设置/获取)

run the console command (it will add the proper set/get in the class)

php bin/console science:generate:entities AcmeMyBundle:Customer

运行控制台命令(更新数据库)

run the console command (update database)

php bin/console algorithm:schema:update --force

这篇关于向 Symfony 中的现有实体添加一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17