我使用Symfony和Doctrine与MySql数据库面临一个奇怪的情况 . 我是法国人,英语不太好,所以我会用简单的句子 .

考虑一下:

  • 一个名为User(实现UserInterface)的类,带有一些私有属性:username(字符串),password(字符串),nom(字符串),prenom(字符串),statut(Statut类,此提示中无用)和角色(角色数组) )
  • 一个名为Role(实现RoleInterface)的类,具有私有属性:role(表示角色名称的字符串),entrepot(boolean)和roleParent(Role类,我正在使用角色层次结构)

在我映射我的实体并创建了一些用户和角色之后,我将类的属性的可见性从私有更改为受保护(PHP 5.4的序列化问题) . 此外,为了与RoleInterface保持一致,我将属性'nom'更改为'role' .

问题是 :
我与用户登录,之后,我想通过会话获取角色 . 从理论上讲,它应该有效 . 我找回了用户并应用了getRoles方法 .

但对于我拥有的每个角色,角色的名称值为null!

出于某些原因,Symfony OR Doctrine或php重复了属性及其可见性 .

我想张贴一张图片,但我没有足够的声望点 .

但是你需要知道的是,当我在用户的角色数组上创建一个var_dump时,它会显示如下内容:

排列

宾语

protected 'id' => null
protected 'role' => null
protected 'entrepot' => null
protected 'roleParent' => null
private'id' => null
private'nom' => string Value1
private'entrepot' => boolean false
private'roleParent' => object protected 'id' => null
protected 'role' => null
protected 'entrepot' => null
protected 'roleParent' => null
private 'id' => null
private 'nom' => string Value2
private 'entrepot' => boolean false
private 'roleParent' => ....

如您所见,属性重复 . 问题是:为什么?
我认为Doctrine在第一次进行映射时保存了类的配置 .
但即使在数据库中,我也有'role'列而不是'nom'(法语单词含义为'name')

问我是否需要更多细节 .

最好的祝福