This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » ERROR:: insert two records to the embeded form table
ERROR:: insert two records to the embeded form table [message #102426] Tue, 13 July 2010 15:10 Go to next message
niroshan  is currently offline niroshan
Messages: 11
Registered: July 2010
Location: Sri Lanka
Junior Member
hi guys,
I`m trying to implement a sign-up form using sfDoctrineGuardPlugin. As for now i have managed to install the plugging and successfully display the forms as well as save (sort of) the data.

But the problem came in saving the data. I have two forms merged together to perform as one sign-up form. while saving 'sfGuardUser' table saves successfully but my other table where i store all the registered user details always insert 2 records. one record with empty data and second record with correct data.

when i debug i found out the empty record been inserted when i call the 'parent::save();' method. but i need it to insert the 'sfGuardUser' table data.

heres what i have done so far,

Schema.yml
----------
sfGuardUserProfile:
  tableName: sf_guard_user_profile
  actAs:        { Timestampable: ~ }
  options:
    type: MyISAM
    collate: utf8_general_ci
    charset: utf8
    use_native_enum: true
  columns:
    id:         { type: integer(4), primary: true, autoincrement: true }
    userId:    { type: integer(4), notnull: true }
    email:
      type: string(100)
      notnull: true
      fixed: true
      unique: true
    siteName:
      type: string(50)
      notnull: true
      fixed: true
    firstName:
      type: string(100)
      notnull: true
      fixed: true
    lastName:
      type: string(100)
      notnull: true
      fixed: true
    gender:
      type: enum(6)
      values: [male, female]
      notnull: true
    dateOfBirth:
      type: date
      notnull: true
    showBirthday:
      type: integer(1)
      notnull: true
    countryId:
      type: integer
      notnull: true
    bio:
      type: text
    hobbies:
      type: text
    activities:
      type: text
    status:
      type: integer(1)
      notnull: true
      default: 1
    views:
      type: integer
      notnull: true
      default: 0
    online:
      type: integer(1)
      notnull: true
      default: 0
    celebrity:
      type: integer
      notnull: true
      default: 0
    currentRating:
      type: integer
      notnull: true
      default: 0
    emailHashKey:
      type: string(50)
      notnull: true
      fixed: true
  relations:
    countries: { local: countryId, foreign: id, foreignAlias: country }
    User:
      class: sfGuardUser
      foreign: id
      local: userId
      type: one
      onDelete: cascade
      foreignType: one
      foreignAlias: Profile

countries:
  actAs: { Timestampable: ~ }
  options:
    type: MyISAM
    collate: utf8_general_ci
    charset: utf8
  columns:
    id: { type: integer(4), primary: true, autoincrement: true }
    name:
      type: string(255)
      notnull: true


//This has nothing but little configuarations
class sfGuardUserProfileForm extends BasesfGuardUserProfileForm
{
  public function configure()
  {    
    // remove the fields that you no longer need
    unset(
     $this['updated_at'],
     $this['created_at'],
	 $this['userId'],
         $this['id'], 
         $this['status'],
	 $this['views'],
	 $this['online'],
	 $this['celebrity'],
	 $this['currentRating'],
	 $this['emailHashKey']
    );
  }
}


//main sfGuardUserForm which embed the above form and do all the saving
class sfGuardUserForm extends PluginsfGuardUserForm
{
  public function configure()
  {
    parent::configure();

    // remove the fields that you no longer need
    unset(
     $this['is_active'],
     $this['is_super_admin'],
     $this['updated_at'],
     $this['groups_list'],
     $this['permissions_list'],
     $this['last_login'],
     $this['created_at'],
     $this['salt'],
     $this['algorithm']
    );

	//name space for the whole form.This will be used to bind the form elements for validations
	$this->widgetSchema->setNameFormat('profile[%s]');	
	
	//changed to password text box
 	$this->widgetSchema['password'] = new sfWidgetFormInputPassword();
	$this->widgetSchema['password_confirmation']    = new sfWidgetFormInputPassword();

    $profileForm = new sfGuardUserProfileForm($this->object->Profile);    
    $this->mergeForm ($profileForm);
  }


  /**
   Override the save method to save the merged user info form.
  */
  public function save($con  = null)
  {  
    parent::save();
	$this->updateUserProfile();
    return $this->object;
  }

  /**
   Updates the user profile merged form.
  */
  protected function updateUserProfile()
  {         
	$userProfObj = new sfGuardUserProfile();
	$values = $this->getValues();

	if ($userProfObj->isNew()){
	  $values['userId'] = $this->object->getId();
	}

	//generate email hash
	$values['emailHashKey'] = md5($values['email']);

	$userProfObj->fromArray($values);
	$userProfObj->save();
	
  }
}


//Action class which call the form
class signupActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  { 
     $params = $request->getParameter('profile');
	 $this->form = new sfGuardUserForm();
	 
	 if ($request->isMethod('post'))
     {
       $this->form->bind($params);

	   if($this->form->isValid())
	   {
		  $this->form->save();

	   }		 
	 }
  }
}


what i`m doing wrong here??

All your thoughts and comments are welcome.

Thanks in advance,

Best regards,
Niroshan

[Updated on: Tue, 13 July 2010 15:11]

Re: ERROR:: insert two records to the embeded form table [message #102457 is a reply to message #102426 ] Wed, 14 July 2010 11:18 Go to previous message
niroshan  is currently offline niroshan
Messages: 11
Registered: July 2010
Location: Sri Lanka
Junior Member
Hi guys,
I have managed to solve the problem. the problem was here,

$profileForm = new sfGuardUserProfileForm($this->object->Profile);


I changed that to,

$profileForm = new sfGuardUserProfileForm(new sfGuardUserProfile);


As a still newbie I`m not quite sure how it resolved but thats did the trick. Symfony experts out there would you mind explain me how it got resolved when i pass 'sfGuardUserProfile' object ???

Thanks in advance,

Best regards,
Niroshan
Previous Topic:ajax response always whole page
Next Topic:session_start() failed to initialize storage module
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software