向 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
相关文章