美文网首页
Doctrine2-更改字段类型

Doctrine2-更改字段类型

作者: Swerve | 来源:发表于2017-04-10 10:21 被阅读0次

Doctrine2 无法更改映射关系。
有时候数据库中的字段类型更改了,同时在zend中的entity里也更改了映射关系,但是无论如何都不会生效。甚至你将这个字段的值改成任意值都可以用

将类型改为任意字符串,生效的依然是第一次起作用的类型
初步感觉是缓存的原因,在网上查到了这样一篇文章Doctrine Annotations里面有一段话

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ApcCache;
$reader = new CachedReader(
new AnnotationReader(),
new ApcCache(),
$debug = true
);```
The debug flag is used here as well to invalidate the cache files when the PHP class with annotations changed and should be used during development.
英语不好,大概意思就是:这里有一个debug标志位,可以设置当php的annotations(注解)改变的时候清除缓存,应该在开发期间使用。

下面还有一句话对上述代码做了更详细介绍

The AnnotationReader works and caches under the assumption that all annotations of a doc-block are processed at once. That means that annotation classes that do not exist and aren’t loaded and cannot be autoloaded (using the AnnotationRegistry) would never be visible and not accessible if a cache is used unless the cache is cleared and the annotations requested again, this time with all annotations defined.
大概意思:AnnotationReader是被作为高速缓存的。一般来说注解只解释一次(这也造成了为什么运行一次之后更改注解无法生效的原因)。除非缓存被清除并且再次请求了注解才会生效(这句翻译可能有误)。

所以解决思路就很明显了,把Doctrine2设置为调试模式。这里我的项目entityManager是用代码创建的,不是用注解,主要是为了数据查询和逻辑代码分离。

public static function getMySQLEntityManager(){
    $isDevMode = true;//重点在这里
    $config = Setup::createAnnotationMetadataConfiguration(
       array(__DIR__ . "/../Entity"),
       $isDevMode, null, null, true);
    $conn = array(
        'host'   => 'host',
        'driver' => 'pdo_mysql', 
        'user' => 'user', 
        'password' => 'password',
        'dbname' => 'name',    );
    return EntityManager::create($conn, $config);}

这段代码创建了一个entityManager对象。Setup::createAnnotationMetadataConfiguration这个方法的第二个参数$isDevMode将这个参数设置为true就可以开启调试模式了。更改过注解之后立即即可生效。如果在ini文件中写的配置应该也是有办法开启调试模式的,请自己搜索解决。

相关文章

  • Doctrine2-更改字段类型

    Doctrine2 无法更改映射关系。有时候数据库中的字段类型更改了,同时在zend中的entity里也更改了映射...

  • SQL常用表操作

    本节要点 表的关键信息更改表名更改字段名更改字段类型更改字段NULL值设置更改字段默认值更改主键字段新增字段删除字...

  • mysql

    字段数据类型: 字段属性: 更改root帐号的密码:

  • 常见错误和技巧

    使用建模里面的模型时,报错:对字段“XXX”指定的类型不充分 解决方法:字段选项——》类型——》读取值 更改字段类...

  • Elasticsearch05重新索引

    ElasticSearch允许我们对一个索引进行字段的新增, 但是不允许我们更改字段的类型或删除一个字段,因为这里...

  • 遇上的坑,记录一下

    ①设置字段time_change的类型为时间戳且默认时间为当前时间,如果行记录发生更改则自动更改time_chan...

  • Git config 常用操作

    查看详细字段 增加信息 删除字段 更改字段

  • flask-migrate 更改表字段类型、字段长度

    想必使用flask-migrate都遇到过数据迁移时相应列的更改的问题,比如更改String的长度的该等,其实al...

  • Oracle转mysql

    1.将表中时间类型的字段更改类型,比如CREATE_TIME,UPDATE_TIME 2.在需要转换的数据库页面点...

  • 常用数据库操作命令

    1.创建库表 2.增加字段 3.更改字段长度 4.修改字段名 5.修改数据类型 6.更新数据 7.新增数据 8.查...

网友评论

      本文标题:Doctrine2-更改字段类型

      本文链接:https://www.haomeiwen.com/subject/wxgfattx.html