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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14941358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:15:08  来源:igfitidea点击:

Add a column to an existing entity in Symfony

phpsymfonydoctrine-orm

提问by TurnsCoffeeIntoScripts

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:

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

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.

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

回答by P. R. Ribeiro

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

实际上,使用 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 是一个 ORM(对象关系映射)工具。这意味着你想从你的 PHP 代码中抽象出数据库,你将数据库的东西委托给 Doctrine。Doctrine 在这方面做得很好。

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.

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

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

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

For Symfony 2:

对于 Symfony 2:

  • 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

  • 通过以下方式修改类:

    • Acme\MyBundle\Entity\Customer 并添加你想要的属性;

      Acme\MyBundle\Entity\Customer

    • 或修改学说文件:

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

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

    php app/console doctrine:generate:entities AcmeMyBundle:Customer

  • 运行控制台命令

    php app/console doctrine:migrations:diff

  • 运行控制台命令(它将在 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:

对于 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

  • 通过以下方式修改类:

    • Acme\MyBundle\Entity\Customer 并添加你想要的属性;

      Acme\MyBundle\Entity\Customer

    • 或修改学说文件:

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

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

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

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

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

回答by Tom

You definitely DO want to open the PHP/XML/YML file where your entity is defined and add the column there. Then, you use the commandline and say

您肯定希望打开定义实体的 PHP/XML/YML 文件并在那里添加列。然后,您使用命令行并说

console doctrine:schema:update

That way, your entity definitions are in sync with the database and your database gets updated.

这样,您的实体定义与数据库同步并且您的数据库得到更新。

回答by Sumit patel

Add new Column in Existing entity on Symfony. I have the same problem are there . after I long research best solutions are there.

在 Symfony 上的现有实体中添加新列。我有同样的问题。在我长期研究最佳解决方案之后。

solution work on Symfony 4

Symfony 4 上的解决方案工作

Example:

例子:

Blog entity already created inside one namecolumn are there and I want to add description column. so simple enter

已经在一个名称列中创建的博客实体在那里,我想添加描述列。这么简单的输入

php bin/console make:entity Blog

After run this command you want to add new column

运行此命令后,您要添加新列

回答by FlameStorm

There is a pitfall on step php app/console doctrine:migrations:diffdescribed above when using doctrine migrations.

php app/console doctrine:migrations:diff使用学说迁移时,上述步骤存在缺陷。

You made changes in entity, all seems valid, but the command on creating migrations php app/console doctrine:migrations:diffsays "No changes detected in your mapping information."

您在实体中进行了更改,一切似乎都有效,但是创建迁移的命令显示php app/console doctrine:migrations:diff“在您的映射信息中未检测到更改”。

And you can't find where's the old database structure placed, search in files found nothing.

而且你找不到旧的数据库结构放在哪里,在文件中搜索一无所获。

For me, the key was to clear Redis cache.

对我来说,关键是清除Redis缓存。

php app/console redis:flushdb

It happens because of config.yml

这是因为 config.yml

snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: redis://localhost
    doctrine:
        query_cache:
            client: default
            entity_manager: default
            namespace: "%kernel.root_dir%"
        metadata_cache:
            client: default
            entity_manager: default
            document_manager: default
            namespace: "%kernel.root_dir%"
        result_cache:
            client: default
            namespace: "%kernel.root_dir%"
            entity_manager: [default, read]  # you may specify multiple entity_managers

After that, migrations:diff reread all of entities (instead of taking outdated ones metadata from cache) and created the right migration.

之后,migrations:diff 重新读取所有实体(而不是从缓存中取出过时的元数据)并创建正确的迁移。



So, the full chain of steps for modifying entities is:

因此,修改实体的完整步骤链是:

  1. Modify your entity class (edit Entity file)
  2. Clear Redis cache of metadata ( php app/console redis:flushdb)
  3. Create (and may be edit) migration ( php app\console doctrine:migrations:diff)
  4. Execute migration ( php app\console doctrine:migrations:migrate)
  1. 修改您的实体类(编辑实体文件)
  2. 清除元数据的Redis缓存( php app/console redis:flushdb)
  3. 创建(并且可能是编辑)迁移 ( php app\console doctrine:migrations:diff)
  4. 执行迁移 ( php app\console doctrine:migrations:migrate)

Of course, your doctrine metadata cache may be not Redis but something else, like files in app/cache or anything other. Don't forget to think about cache clearing.

当然,您的学说元数据缓存可能不是 Redis,而是其他内容,例如 app/cache 中的文件或其他任何内容。不要忘记考虑缓存清除。

May be it helps for someone.

可能对某人有帮助。

回答by Kunal

FOR SYMFONY3 USERS...

对于 SYMFONY3 用户...

here you have to follow two steps to make changes in your entity Step1: Open Your Entity File..For EX: "Acme\MyBundle\Entity\Book"

在这里,您必须按照两个步骤对您的实体进行更改 Step1:打开您的实体文件..对于 EX:“Acme\MyBundle\Entity\Book”

Current Entity is having few fields like:id,name,title etc. Now if you want to add new field of "image" and to make change constraint of "title" field then add field of "image" with getter and setter

当前实体有几个字段,如:id、name、title 等。现在如果你想添加“图像”的新字段并更改“标题”字段的约束,然后使用 getter 和 setter 添加“图像”字段

/**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=500)
     */
    private $image;

And add getter and setter

并添加 getter 和 setter

/**
     * Set image
     *
     * @param string $image
     *
     * @return Book
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getimage()
    {
        return $this->image;
    }

To update existing field constraint of title from length 255 to 500

将标题的现有字段约束从长度 255 更新为 500

/**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=500)
     */
    private $title;

Ok...You have made changes according to your need,And You are one step away.

好的...您已经根据您的需要进行了更改,您就在一步之遥。

Step 2: Fire this command in project directory

第 2 步:在项目目录中触发此命令

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

Now,check your table in database ,It's Done!!

现在,检查数据库中的表,完成!!

回答by Mihir Bhende

I found a way in which I added the new column inside YourBundle/Resources/Config/doctrine/Yourentity.orm.yml.

我找到了一种在 YourBundle/Resources/Config/doctrine/Yourentity.orm.yml 中添加新列的方法。

Then added getter and setter methods inside Entity class.

然后在 Entity 类中添加 getter 和 setter 方法。

Then did php app/console doctrine:schema:update --forcefrom console. It worked for me.

然后php app/console doctrine:schema:update --force从控制台做。它对我有用。

回答by Mayukh Roy

I think, you need to update your relation table manually to map the relations. I found this:discussion

我认为,您需要手动更新关系表以映射关系。我发现了这个:讨论

Again, We can generate entities from existing database, as a whole, but separately? :(

同样,我们可以从现有数据库中生成实体,作为一个整体,但单独生成?:(

回答by Tech Nomad

That's how it worked for me:

这就是它对我的工作方式:

  • add new vars, setters and getters inside the existing class:
  • 在现有类中添加新的 var、setter 和 getter:
    src-->AppBundle-->Entity-->YourClassName.php
  • update the ORM File:
  • 更新 ORM 文件:
  src-->AppBundle-->Resources-->config-->doctrine-->YourClassName.orm.yml
  • run bash command:
  • 运行 bash 命令:
  php bin/console doctrine:schema:update --force