向Symfony中的现有实体添加一列 [英] Add a column to an existing entity in Symfony
问题描述
php应用程序/控制台原则:修改:实体
现在我知道这个命令不存在,但是有一种方法没有做整个迁移)只需添加列。
PS我知道我可以打开php文件,并在文本中添加列,然后更新模式,但是我将它分发给一些客户端,我喜欢更多的类命令行方法。
实际上,使用Doctrine完全没有意义,可以做你喜欢的建议。
原则是一个ORM(对象关系映射)工具。这意味着您要从您的PHP代码抽象数据库,您将数据库内容委托给Doctrine。原则在这一领域做得非常出色。
如果您希望使用最新版本的模型来保持客户/同行更新,那么您应该使用原则迁移(< a href =http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html =nofollow noreferrer> http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html )。这是管理数据库更新的方式。此外,它可以完全控制升级/降级数据库时该怎么做。您可以在添加FK之前设置默认值。
在类上添加新属性的步骤应为:
-
通过以下方式修改课程:
-
Acme\ MyBundle\Entity\Customer并添加您想要的属性;
Acme\MyBundle\Entity\Customer
-
或修改原则文件:
Acme / MyBundle /Resources/config/doctrine/customer.yml
-
-
运行控制台命令(它将在类中添加正确的set / get)
php app / console doctrine:generate:entities AcmeMyBundle:Customer
-
运行控制台命令
控制台原则:迁移:diff
-
运行控制台命令(它将在app / DoctrineMigrations上放置一个新文件)
php应用程序/控制台原则:迁移:迁移
当您部署新版本的代码时,您所要做的就是更新源代码并运行上面的命令。
对于Symfony 3:
-
通过以下方式修改课程:
-
Acme\MyBundle\Entity\Customer并添加你想要的财产;
Acme\MyBundle\Entity\Customer
-
或修改原则文件:
Acme / MyBundle / Resources / config / doctrine / customer.yml
-
-
运行控制台命令(它将添加正确的set / get在课堂上)
php bin / console doctrine:generate:entities AcmeMyBundle:Customer
-
运行控制台命令(更新数据库)
php bin / console doctrine:schema:update --force
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.
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.
Actually, using Doctrine does not make sense at all to do something like you suggested.
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.
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:
modify the class by:
Acme\MyBundle\Entity\Customer and add the property you want;
Acme\MyBundle\Entity\Customer
or modify the doctrine file:
Acme/MyBundle/Resources/config/doctrine/customer.yml
run the console command (it will add the proper set/get in the class)
php app/console doctrine:generate:entities AcmeMyBundle:Customer
run the console command
php app/console doctrine:migrations:diff
run the console command (it will place a new file on app/DoctrineMigrations)
php app/console doctrine:migrations:migrate
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.
For Symfony 3:
modify the class by:
Acme\MyBundle\Entity\Customer and add the property you want;
Acme\MyBundle\Entity\Customer
or modify the doctrine file:
Acme/MyBundle/Resources/config/doctrine/customer.yml
run the console command (it will add the proper set/get in the class)
php bin/console doctrine:generate:entities AcmeMyBundle:Customer
run the console command (update database)
php bin/console doctrine:schema:update --force
这篇关于向Symfony中的现有实体添加一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!