问题
I am trying to add a custom field to the user form in the sfGuard plugin.
I have created the column in the database and added the widgetschema for the field. It is showing up correctly in the form, and when text is added and submitted, it is showing the update was successful. The data inserted into the field is not getting populated in the database though.
I am thinking I have to update the model for sfGuard, as it is not aware of the new field. I have tried $ ./symfony doctrine:build-model
, but that does not seem to update classes in the sfGuard plugin.
I have added this to sfGuardUserAdminClass :
$this->widgetSchema['department_title'] = new sfWidgetFormInputText();
$this->validatorSchema['department_title'] = new sfValidatorString(array());
I can see the form field and submit, it is just not catching and inserting the data into the db.
UPDATE
I have updated my schema with the MyUser object, migrated the diff and built the model. I added $this->embedRelation('Profile');
in the sfGuardUserAdminForm.class. I am getting this error
"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:
If you really want to add a field to the user model you have to modify the schema.yml config in plugins/sfDoctrineGuardPlugin/config/doctrine/schema.yml
. Find the table sfGuardUser and add your column. But this is not the good way to go as if you update the plugin, you will lost your changes.
Best is to create a dedicated table which will be linked to the sfGuardUser model with a one-to-one relation. In your config/doctrine/schema.yml create table 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
After rebuilding all, the custom properties will be accessible via:
$context->getUser()->getProfile()->getDepartementTitle();
And you can embed the generated MyUserProfileForm form into your sfGuardUserAdminForm form:
$this->embedRelation('Profile');
来源:https://stackoverflow.com/questions/11024158/adding-a-new-user-field-to-sfguard-user-admin-form