php Symfony 学说:模式:更新不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19897915/
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 20:12:15  来源:igfitidea点击:

Symfony doctrine:schema:update not working

phpsymfonydoctrine-orm

提问by G. Trennert

I have a strange problem : I have an application symfony 2.3 (with sonata user) I created a bundle with one entity - the entity was created without a problem then I had to modify the entity and now it seems to be impossible to modify the schema :

我有一个奇怪的问题:我有一个应用程序 symfony 2.3(与奏鸣曲用户)我创建了一个包含一个实体的包 - 创建实体没有问题,然后我不得不修改实体,现在似乎无法修改架构:

To see what happens I increased all the string lengths with +1

为了看看会发生什么,我用 +1 增加了所有的字符串长度

The entity code (with annotations) :

实体代码(带注释):

namespace Too\ConfigAppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * ConfigApp
 *
 * @ORM\Table(name="ConfigApp")
 * @ORM\Entity(repositoryClass="Too\ConfigAppBundle\Entity\ActiviteRepository")
 */
class ConfigApp
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $nom
     *
     * @ORM\Column(name="nom", type="string", length=101, unique=true)
     */
    private $nom;

    /**
     * @var string $nomSlug
     *
     * @Gedmo\Slug(fields={"nom"}, updatable=true, separator="_")
     * @ORM\Column(name="nomSlug", type="string", length=101, nullable=true)
     */
    private $nomSlug;

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

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

    /**
     * @var datetime $cree_le
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="cree_le", type="datetime")
     */
    private $cree_le;

    /**
     * @var datetime $modifie_le
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modifie_le", type="datetime")
     */
    private $modifie_le;

    ...

Now see the result of :

现在看结果:

php app/console doctrine:schema:update --dump-sql

CREATE TABLE ConfigApp (id INT AUTO_INCREMENT NOT NULL, nom VARCHAR(100) NOT NULL, nomSlug VARCHAR(100) NOT NULL, email VARCHAR(150) NOT NULL, telephone VARCHAR(15) NOT NULL, cree_le DATETIME NOT NULL, modifie_le DATETIME NOT NULL, PRIMARYKEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB

None of the new length is taken in account : for example the field nom should have length=101, but the dump-sql gives nom VARCHAR(100) !

没有考虑新的长度:例如字段 nom 应该有 length=101,但是 dump-sql 给出了 nom VARCHAR(100) !

Could anyone try to figure out whats going wrong ? Thanks !

任何人都可以尝试弄清楚出了什么问题吗?谢谢 !

EDIT : I tried to clear the cache before with : * php app/console doctrine:cache:clear-metadata * php app/console cache:clear * by deleting all content in cache folders

编辑:我之前尝试清除缓存: * php app/console algorithm:cache:clear-metadata * php app/console cache:clear * 通过删除缓存文件夹中的所有内容

I also tried --dump-sql and --force.

我也试过 --dump-sql 和 --force。

This changes nothing at all. Please any hint would be welcome !

这根本没有改变。请任何提示将受到欢迎!

回答by Yvan

I just ended up on the exact same issue: schema doesn't update.

我刚刚结束了完全相同的问题:架构没有更新。

Note that --force returns exactly the same thing as --dump-sql, the only difference is that --force runs the SQL against the database.

请注意 --force 返回的内容与 --dump-sql 完全相同,唯一的区别是 --force 针对数据库运行 SQL。

Although, in my case, the issue wasn't because of the .orm.xml file. It was because I've set this in the config_dev.xml:

虽然就我而言,问题不是因为 .orm.xml 文件。这是因为我在 config_dev.xml 中设置了这个:

doctrine:
orm:
    metadata_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached
    query_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached
    result_cache_driver:
        type: memcached
        host: localhost
        port: 11211
        instance_class: Memcached

Even when I issue an often salvatory:

即使我经常发出救赎的话:

php app/console cache:clear

the memcached data isn't flushed. So I had to restart memcached, then everything was up and running again!

memcached 数据未刷新。所以我不得不重新启动 memcached,然后一切又开始运行了!

So thanks for your question, it led me to the right spot in my case.

所以谢谢你的问题,它把我带到了正确的地方。

UPDATE: as Phil suggested above, running this command does the trick too:

更新:正如 Phil 上面建议的那样,运行此命令也可以解决问题:

php app/console doctrine:cache:clear-metadata

回答by Saman Mohamadi

It is possible that you forget to enable Doctrine auto mapping;

您可能忘记启用 Doctrine 自动映射;

orm:
   #auto_mapping: true

If auto mapping is disabled (or commented like above) , you should register Entities of each bundle manually.

如果自动映射被禁用(或如上评论),您应该手动注册每个包的实体。

orm:
   entity_managers:
      default:
         mappings:
            AcmeHelloBundle: ~

回答by G. Trennert

Try to use YAML instead of the default annotation when you run

运行时尝试使用 YAML 而不是默认注释

php app/console doctrine:generate:entity

Or instead of running

或者代替跑步

app/console doctrine:schema:update --force

You can just manually create your MySql table which is a very tedious task

您可以手动创建 MySql 表,这是一项非常繁琐的任务

回答by G. Trennert

I found the solution : I did not see before but there was a doctrine folder in src\Too\ConfigAppBundle\Resources\config containing a file called ConfigApp.orm.yml :

我找到了解决方案:我之前没有看到,但在 src\Too\ConfigAppBundle\Resources\config 中有一个包含一个名为 ConfigApp.orm.yml 的文件的学说文件夹:

Too\ConfigAppBundle\Entity\ConfigApp:
    type: entity
    table: null
    repositoryClass: Too\ConfigAppBundle\Entity\ConfigAppRepository
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        nom:
            type: string
            length: '100'
        nomSlug:
            type: string
            length: '100'
        email:
            type: string
            length: '150'
        telephone:
            type: string
            length: '15'
        cree_le:
            type: datetime
            length: null
        modifie_le:
            type: datetime
            length: null
    lifecycleCallbacks: {  }

I deleted this folder and now updating the schema works again.

我删除了这个文件夹,现在更新架构再次起作用。

Surely I did something to generate this doctrine folder but I don't know what it was - if someone could tell me how this stuff is generated - and why ?

当然,我做了一些事情来生成这个学说文件夹,但我不知道它是什么——如果有人能告诉我这些东西是如何生成的——为什么?

回答by psiess

i think its beacause of the doctrine:mapping:import command. This command stores the schema of an existing database into .orm.xml files. Probebly you execute this command.

我认为这是由于学说:映射:导入命令。此命令将现有数据库的架构存储到 .orm.xml 文件中。可能你执行了这个命令。

I had the same issue, coast me a lot of time to find out.

我有同样的问题,我花了很多时间来找出答案。

回答by goulashsoup

Because I was using .orm.yml-mapping I had the issue that I created the doctrine-folder, in which the ymlmappings were present, under the wrong path, so I fixed it by moving the doctrine-folder to the the configfolder: ...\BundleName\Resources\config\doctrine\MyEntity.orm.yml

因为我使用的.orm.ymldoctrine-mapping yml,所以我在错误的路径下创建了 - 文件夹,其中存在映射,所以我通过将 -文件夹移动doctrineconfig文件夹来修复它: ...\BundleName\Resources\config\doctrine\MyEntity.orm.yml

回答by SandMania

Though some of the answers given by @rai and others are correct, one more suggestion for Symfony version is equal to or above 3.0 please use bin/console instead app/console, as shown below,

虽然@rai 和其他人给出的一些答案是正确的,但对于 Symfony 版本等于或高于 3.0 的另一个建议请使用 bin/console 代替 app/console,如下所示,

bin/console doctrine:schema:update --force

回答by S.Thiongane

Type php app/console help doctrine:schema:updatein CLI

类型php app/console help doctrine:schema:update的CLI

 --dump-sql            Dumps the generated SQL statements to the screen (does no
t execute them).

...

 --force               Causes the generated SQL statements to be physically exec
uted against your database.

So try the --forceinstead of --dump-sql.

所以尝试--force代替--dump-sql.

And here is the command for cache clearing :

这是清除缓存的命令:

php app/console cache:clear

Don't forget to use the helpkeyword before a command namespace in order to get the help message for that command.

不要忘记help在命令命名空间之前使用关键字,以便获取该命令的帮助消息。

Hope it helps

希望能帮助到你

回答by bsnrijal

Try

尝试

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

this is update your database schema with entity

这是使用实体更新您的数据库架构