【问题标题】:Adding a new user field to sfGuard user Admin Form向 sfGuard 用户管理表单添加新用户字段
【发布时间】:2012-06-13 22:20:41
【问题描述】:

我正在尝试在 sfGuard 插件的用户表单中添加自定义字段。

我已经在数据库中创建了列,并为该字段添加了 widgetschema。它在表单中正确显示,当添加和提交文本时,它显示更新成功。但是,插入该字段的数据并未填充到数据库中。

我想我必须更新 sfGuard 的模型,因为它不知道新字段。我试过$ ./symfony doctrine:build-model,但这似乎并没有更新 sfGuard 插件中的类。

我已将此添加到 sfGuardUserAdminClass :

$this->widgetSchema['department_title'] = new sfWidgetFormInputText();
$this->validatorSchema['department_title'] = new sfValidatorString(array());

我可以看到表单字段并提交,它只是没有捕获数据并将数据插入到数据库中。

更新

我已经使用 MyUser 对象更新了我的架构,迁移了差异并构建了模型。我在 sfGuardUserAdminForm.class 中添加了$this->embedRelation('Profile');。我收到此错误

"Catchable fatal error: Argument 1 passed to sfUser::__construct() must be an instance of sfEventDispatcher, instance of MyUserTable given, called in /Users/careyestes/Sites/sva/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Table.php on line 301 and defined in /Users/careyestes/Sites/sva/lib/vendor/symfony/lib/user/sfUser.class.php on line 46 Fatal error: Call to a member function evictAll() on a non-object in /Users/careyestes/Sites/sva/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php on line 1239"

【问题讨论】:

  • 对不起,MyUser 的名称可能是错误的,因为它也是 sfUser 的默认类的名称。尝试在架构中将名称从 MyUser 更改为 MyUserProfile。我更新了我的答案。

标签: symfony1 symfony-1.4 symfony-forms sfguard


【解决方案1】:

如果你真的想在用户模型中添加一个字段,你必须修改 plugins/sfDoctrineGuardPlugin/config/doctrine/schema.yml 中的 schema.yml 配置。找到表 sfGuardUser 并添加您的列。但这不是好方法,因为如果您更新插件,您将丢失所做的更改。

最好创建一个专用表,该表将通过一对一的关系链接到 sfGuardUser 模型。在您的 config/doctrine/schema.yml 创建表 MyUserProfile:

MyUserProfile:
  columns:
    id: { type: integer, primary: true, autoincrement: true }
    sf_guard_user_id: { type:integer }
    department_title: { type: string(255) }
    # ... other column definitions
  relations:
    User:
     class: sfGuardUser
     foreignType: one
     foreignAlias: Profile
     local: sf_guard_user_id
     onDelete: CASCADE

全部重建后,自定义属性将可通过以下方式访问:

$context->getUser()->getProfile()->getDepartementTitle();

您可以将生成的 MyUserProfileForm 表单嵌入到您的 sfGuardUserAdminForm 表单中:

$this->embedRelation('Profile');

【讨论】:

  • 我通过更新 sfguard 架构让它工作了。我不知道如果我升级,我会丢失那些架构设置。我将把它投入开发。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 2010-10-28
  • 1970-01-01
  • 2021-03-07
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多