将新用户字段添加到sfGuard用户管理表单

时间:2012-06-13 22:20:42

标签: symfony1 symfony-1.4 symfony-forms sfguard

我正在尝试在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"

1 个答案:

答案 0 :(得分: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');
相关问题