1

I really got a weird problem where a user register symfony save 2 time in the sf_guard_user table (one empty) and link the profile to the empty row.

I do not hit submit form 2 time, and the action is only executed once. The debug bar tell me that it really save 2 time in the DB, once with empty data.

Really frustrating

Schema:

 BambinbazarUsers:
  actAs:
    Geographical: ~
  columns:
    sf_guard_user_id: integer
    company:   { type: string(255) }
    url:       { type: string(255) }   
    usertype:  { type: string(255) }
    address:   { type: string(255) } 
    city:      { type: string(255) }
    facebookid:{ type: string(255)}
    state:     { type: string(255) } 
    zipcode:   { type: string(255) }  
    Country:   { type: string(255) }          
    telephone: { type: string(255) }      
  relations:   
   BambinbazarArticles:
     class: BambinbazarArticles
     local: sf_guard_user_id
     foreign: userid
     type: many
     foreignType: one
   sfGuardUser:
      onDelete: CASCADE
      local: sf_guard_user_id
      foreign: id
      foreignAlias: BambinbazarUsers
      foreignType: one    

Action (nor changed) :

 if ($this->getUser()->isAuthenticated())
    {
      $this->getUser()->setFlash('notice', 'You are already registered and signed in!');
      $this->redirect('@homepage');
    }
    $this->form = new sfGuardRegisterForm();

    if ($request->isMethod('post'))
    {

      $this->form->bind($request->getParameter($this->form->getName()));
      if ($this->form->isValid())
      {

        $user = $this->form->save();

        $this->getUser()->signIn($user);

        $this->redirect('@view_my_profile');
      }
    }

form configure:

public function configure()
  {
      $profileForm = new BambinbazarUsersForm($this->object->BambinbazarUsers);
      unset($profileForm['id'], $profileForm['sf_guard_user_id']);
      $this->embedForm('Profile', $profileForm);

  }
The Orca
  • 1,250
  • 2
  • 17
  • 31
  • 1
    off-topic (or maybe not?), but you should remove the 's' after BambinbazarUser you could have some problems when doctrine detects relation types (one-to-many, one-to-one). A doctrine class name should always be singular I think. – greg0ire Feb 07 '11 at 19:35
  • greg0ire is right. Everything about the grammar of the generated code works out better if your schema has singular names. – Nathan Feb 07 '11 at 20:44

2 Answers2

0

sfGuard's signIn() method also saves the user object, in order to update the last login time:

public function signIn($user, $remember = false, $con = null)
{
  // signin
  $this->setAttribute('user_id', $user->getId(), 'sfGuardSecurityUser');
  $this->setAuthenticated(true);
  $this->clearCredentials();
  $this->addCredentials($user->getAllPermissionNames());

  // save last login
  $user->setLastLogin(time());
  $user->save($con);
  // ...

If you do the signIn() before you save the form does it work?

Nathan
  • 3,842
  • 1
  • 26
  • 31
  • Well I tried adding a die before the redirect, the sql insert is still executed 2 times, I did not know about the S problem, maybe it, then again, all other relations are working – The Orca Feb 08 '11 at 01:38
0

I found the problem

I had an embedded relation related to the sfGuard user table in BambinbazarUsersForm.

Effectively creating an empty record.

The solution was to create a profile form embedding the base user form, a newbie error I guess, but really hard to debug...

The Orca
  • 1,250
  • 2
  • 17
  • 31