【问题标题】:Symfony2 using Multiuser Bundle and Alice BundleSymfony2 使用 Multiuser Bundle 和 Alice Bundle
【发布时间】:2016-03-07 06:23:42
【问题描述】:

在我的网络项目中,我一直在使用 Hautelook Alice Bundle (https://github.com/hautelook/AliceBundle) 来创建用于开发和测试的装置。对于用户管理,我使用 FOS UserBundle。 我的数据文件看起来有点像这样,它曾经工作得非常好:

AppBundle\Entity\StaffDetail:
  staffdetail_{1..5}:
    prefix: <title()>
    first_name: <firstName()>
    last_name: <lastName()>

AppBundle\Entity\Staff:
  staff_1:
    username: admin@example.com
    plainpassword: password
    email: admin@example.com
    mandant: @mandant_1
    enabled: true
    role: administrator
    staffdetail: @staffdetail_1
  staff_{2..5}:
    username (unique): <safeEmail()>
    plainpassword: password
    email: <identity($username)>
    mandant: @mandant_1
    enabled: true
    role: administrator
    staffdetail: @staffdetail_<current()>

现在我正在尝试为不同的用户组(员工和客户)设置不同的登录机制,所以我想使用 Rollerworks 多用户捆绑包 (https://github.com/rollerworks/RollerworksMultiUserBundle)。

我为客户和员工创建了一个新捆绑包并设置了所有内容。使用该应用程序时,一切正常。但是生成固定装置不再起作用。

在数据文件中只是命名空间发生了变化。

Users\StaffBundle\Entity\StaffDetail:
  staffdetail_{1..5}:
    prefix: <title()>
    first_name: <firstName()>
    last_name: <lastName()>

Users\StaffBundle\Entity\Staff:
  staff_1:
    username: admin@example.com
    plainpassword: password
    email: admin@example.com
    mandant: @mandant_1
    enabled: true
    role: administrator
    staffdetail: @staffdetail_1
  staff_{2..5}:
    username (unique): <safeEmail()>
    plainpassword: password
    email: <identity($username)>
    mandant: @mandant_1
    enabled: true
    role: administrator
    staffdetail: @staffdetail_<current()>

当我运行 fixtures 命令(或运行同时执行 generate fixtures 命令的测试)时,它看起来只是试图将值插入数据库而不首先运行实体的 FOS UserBundle 构造函数,作为密码,未设置 username_canonical 等。

[Doctrine\DBAL\DBALException]
  An exception occurred while executing 'INSERT INTO staff (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_tok
  en, password_requested_at, roles, credentials_expired, credentials_expire_at, role, staffdetail_id, mandant_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["ad
  min@example.com", null, "admin@example.com", null, 1, "kbttb26978gkkcosk404wccgc4os8wo", null, null, 0, 0, null, null, null, "a:0:{}", 0, null, "administrator", 22, 13]:
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username_canonical' cannot be null

我这几天都被这个问题困住了,有人有什么建议吗?

谢谢

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    usernameCanonical 添加到您的灯具中:

    Users\StaffBundle\Entity\Staff:
        staff_1:
            usernameCanonical: <identity($username)>
    

    这同样适用于其他员工条目。您的 User 可能具有根据用户名值自动填充它的逻辑。

    确保您的Users\StaffBundle\Entity\Staff 实现FOS\UserBundle\Model\UserInterfaceFOS\UserBundle\Doctrine\UserListener 被调用(它会在保存用户之前调用updateCanonicalFieldsupdatePassword)。

    在这里阅读更多:http://symfony.com/doc/current/bundles/FOSUserBundle/user_manager.html#updating-a-user-object

    【讨论】:

    • 我已经考虑过了,虽然这可能适用于 username_canonical 或 email_canonical 等字段,但不适用于从 plainpassword 字段加密的密码。
    • 啊,所以还有更多属性未填写 - 您需要向我们展示您的实体。
    • 看起来没有调用 setter - 它只是直接填充提供的属性
    • 我很确定没有调用 Fos 用户的 Doctrine Listener:github.com/FriendsOfSymfony/FOSUserBundle/blob/… 在此处阅读更多内容:github.com/FriendsOfSymfony/FOSUserBundle/blob/…
    • 谢谢,今晚晚些时候我会去看看。但是我的实体扩展了FOS\UserBundle\Model\User,它实现了FOS\UserBundle\Model\UserInterface
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2014-03-28
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多