简体   繁体   English

symfony2或主义正在从我的实体中删除用户名字段-为什么?

[英]symfony2 or doctrine are removing username field from my entities — why?

Update: It's not just username that's this affects. 更新:不仅会影响username ,还会影响username I even tried renaming the column to foo in my schema, and still it always returns null for that column! 我什至尝试在我的架构中将列重命名为foo ,但仍然始终为该列返回null

My database is in MySQL (InnoDB). 我的数据库在MySQL(InnoDB)中。 I have a users table, containing the properties ID , username , name , email , password . 我有一个users表,其中包含属性IDusernamenameemailpassword Table has a bunch of users inserted in it. 表中插入了一堆用户。

On the Doctrine side, my AppBundle\\Entity\\User class is correctly defined. 在原则方面,正确定义了我的AppBundle\\Entity\\User类。 I can query my users and get a list of them, with all the columns intact -- except for the username column. 我可以查询我的用户,并让他们的名单,所有完整的列-除了username列。

Despite username being defined in PHP: 尽管在PHP中定义了username

/**
 * User
 *
 * @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})})
 * @ORM\Entity
 */
class User
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45,  nullable=false)
     */
    private $username;

    // ...


    /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

When I get the entities and dump them, the username field has mysteriously been nulled: 当我获取实体并转储它们时,用户名字段已神秘地为空:

User {#389 ▼
  -id: 2
  -username: null
  -name: "Sam"
  -password: XXX
  -email: XXX
  -oPhone: ""
  -mPhone: XXX
  -hPhone: ""
  -institutions: PersistentCollection {#390 ▶}
}

Even though it definitely exists and is not null on the database: 即使它确实存在并且在数据库上也不为空:

mysql> SELECT * FROM user WHERE id=2

# ID, username, name, password, email, o_phone, m_phone, h_phone
2, sjw, Sam, XXX, XXX, , XXX, 

And trying to access the field via PHP results in it complaining that no such username field exists, preventing me from doing user auth using this entity. 并试图通过PHP导致它抱怨,没有这样的访问字段username字段存在,阻止了我这样做使用该实体的用户认证。

  • I'm not using any custom app bundles in Symfony, which is version 2.7.6 (with doctrine2), on PHP 5.4. 我未在PHP 5.4上的Symfony(版本2.7.6(带有doctrine2))中使用任何自定义应用程序捆绑包。

  • I've cleared my caches, made sure I haven't had any Doctrine-generated .yml or .xml models lying around. 我已经清除了缓存,确保没有任何Doctrine生成的.yml或.xml模型。

  • I've sanity-checked my database -- using the sqlalchemy Python library and sqlacodegen I have a python version of my ORM. 我已经对数据库进行了完整性检查-使用sqlacodegen Python库和sqlacodegen我有ORM的python版本。 That has no problems with the username column, results appear as they should, rather than being null or NoneType. username名列没有问题,结果按预期显示,而不是null或NoneType。

  • I've temporarily disabled the SecurityBundle to see if it was removing the username behind the scenes -- the field is still nulled. 我暂时禁用了SecurityBundle,以查看它是否正在删除幕后的用户名-该字段仍为空。

  • I've pored over the Symfony docs... 我已经仔细研究过Symfony文档...

Nope, out of ideas. 不,出于想法。 What is Symfony doing behind the scenes? Symfony在幕后做什么?

So it turns out it was a cache problem, as the chosen answer here explains. 因此,事实证明这是一个缓存问题,如此处选择的答案所解释。 I completely forgot that I had enabled APC to cache database entries! 我完全忘记了我已经启用了APC来缓存数据库条目! Turns out it was also caching the database metadata and various bits of code, so even when I was clearing everything else out, the APC cache didn't get updated. 事实证明,它还在缓存数据库元数据和各种代码,因此即使我清除了所有其他内容,APC缓存也没有得到更新。

This was the culprit, these lines in config.yml which should only have been in config_prod.yml : 这是罪魁祸首,这些行在config.yml中应该config_prod.yml

    doctrine:
        orm:
            metadata_cache_driver: apc
            result_cache_driver: apc
            query_cache_driver: apc

(Another solution might have been to just restart php-fpm , as that's how I'm running PHP in my environment -- something that would have taken a second to do but I didn't think of for a whole two days... doh...) (另一种解决方案可能只是重新启动php-fpm ,因为这就是我在环境中运行PHP的方式-可能需要花一秒钟的时间,但是我整整两天都没想到... h ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM