I'm working on a module that uses sfDoctrineGuard
plugin as base (means uses sfDoctrineGuard) so in my module I developed this code:
class UsuariosForm extends sfGuardUserForm {
protected $current_user;
public function configure() {
unset(
$this['is_super_admin'], $this['updated_at'], $this['groups_list'], $this['permissions_list'], $this['last_login'], $this['created_at'], $this['salt'], $this['algorithm']
);
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
$this->setDefault('idempresa', $id_empresa);
$this->current_user = sfContext::getInstance()->getUser()->getGuardUser();
$this->validatorSchema['idempresa'] = new sfValidatorPass();
$this->widgetSchema['first_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['last_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['username'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['email_address'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->validatorSchema['password']->setOption('required', true);
$this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];
$this->widgetSchema->moveField('password_confirmation', 'after', 'password');
$this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirmation', array(), array('invalid' => 'The two passwords must be the same.')));
}
public function save($con = null) {
if (sfContext::getInstance()->getActionName() == "create" || sfContext::getInstance()->getActionName() == "new") {
$new_user = parent::save($con); /* @var $user sfGuardUser */
$new_user->addGroupByName('Monitor');
}
return $new_user;
}
}
The first function allow me to have my own form instead of sfDoctrineGuard plugin form and the second one is a override of save()
method for add a default group to the new users I'm creating. I want also add a default idempresa
as you may notice (in config()
function) but it's not working, maybe I'm doing something wrong or don't know. idempresa
is a field stored in a sfGuardUserProfile
table and have of course the relations configured and so on. My question here is: what should be the right way to setup the default idempresa
in order to set the profile when users are created?
You have to save the $new_user
object again: $new_user->save($con)
Also you don't have to check for action_name in the save() method, you can check it the object is new or not. The Objectform has a method for that.
<?php
...
public function save($con = null)
{
$new_user = parent::save($con);
if($this->isNew())
{
$new_user->addGroupByName('Monitor');
$new_user->save($con); //this saves the group
}
return $new_user;
}
...