Symfony doctrine:schema:update无法正常工作

11

我有一个奇怪的问题: 我有一个应用程序symfony 2.3(带有Sonata用户) 我创建了一个包含一个实体的bundle - 实体创建没有问题 然后我不得不修改实体,现在似乎无法修改模式:

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

实体代码(带有注释):

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;

    ...

现在看一下结果:

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

新长度没有被考虑在内: 例如,名称字段应该具有长度为101, 但是dump-sql给出的名称VARCHAR(100)!

有人能想出问题出在哪里吗? 谢谢!

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

我还尝试过使用--dump-sql和--force。

这完全没有改变。 请提供任何提示!


5
我会尝试清除元数据缓存。我认为命令是 doctrine:cache:clear-metadata。只需运行 app/console 获取命令列表。 - Phil
9个回答

15

我遇到了和你一模一样的问题:模式(schema)没有更新。

需要注意的是,--force 和 --dump-sql 返回的内容完全相同,唯一的区别是 --force 会将 SQL 运行在数据库上。

不过,在我的情况下,问题并不是由于 .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

即使我经常发出拯救性的呼声:

php app/console cache:clear

缓存数据没有被清除,所以我不得不重启缓存,然后一切都恢复正常了!

所以感谢您的问题,在我的情况下让我找到了正确的解决方法。

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

php app/console doctrine:cache:clear-metadata

11

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

orm:
   #auto_mapping: true
如果禁用了自动映射(或像上面一样被注释掉),则应该手动注册每个bundle的Entities。
orm:
   entity_managers:
      default:
         mappings:
            AcmeHelloBundle: ~

7

在运行时尝试使用YAML代替默认注释。

php app/console doctrine:generate:entity

或者您可以选择不运行

app/console doctrine:schema:update --force

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

2
我发现解决方法: 我之前没有看到,但在src\Too\ConfigAppBundle\Resources\config中有一个名为ConfigApp.orm.yml的文件夹包含Doctrine文件夹:
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: {  }

我删除了这个文件夹,现在更新架构就可以正常工作了。

当然,我做了一些事情才生成了这个doctrine文件夹,但我不知道是什么 - 如果有人能告诉我这个东西是如何生成的以及为什么要这样做?


2

虽然@rai和其他人给出的答案是正确的,但对于Symfony版本等于或高于3.0,请使用bin/console而不是app/console,如下所示:

bin/console doctrine:schema:update --force

1
我觉得这是由于doctrine:mapping:import命令导致的。该命令将现有数据库的模式存储到.orm.xml文件中。你可能执行了这个命令。
我曾经遇到过同样的问题,找了很长时间才发现。

1
因为我使用了.orm.yml映射,所以我遇到了这样一个问题,我创建了一个doctrine文件夹,其中包含yml映射,但是路径错误,所以我通过将doctrine文件夹移动到config文件夹中来解决它: ...\BundleName\Resources\config\doctrine\MyEntity.orm.yml

1
能否请下投票者解释一下为什么要投反对票?当然这不是问题的答案,但我只是添加了一个可能被忽略的解决方案,以便在寻找答案时有所帮助... - goulashsoup

-1

尝试一下

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

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


这并没有太大帮助,因为它会转储与--dump-sql完全相同的信息。 - Yvan

-1
在CLI中输入php app/console help doctrine:schema:update
 --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.

所以尝试使用--force而不是--dump-sql

这是清除缓存的命令:

php app/console cache:clear

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

希望能对您有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接