Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Create profile after adding a new user, the simple way.

Sometimes if you use sfGuardUser and sfGuardUserProfile you need to create an empty profile just after adding the new user (automating is such a great thing). This snippet will help out.

UPDATE: Will Killian pointed me to the solution of using this without touching the plugin files, this way it will still work after upgrading the plugin. Thanks!

Edit apps/backend/modules/sfGuardUser/actions/actions.class.php

(create directories and files as needed, just for the record I use this with admin generator on "backend" app)

Mine looks like this.

<?php
 
require_once(sfConfig::get('sf_plugins_dir').'/sfGuardPlugin/modules/sfGuardUser/lib/BasesfGuardUserActions.class.php');
 
class sfGuardUserActions extends BasesfGuardUserActions
{
  protected function savesfGuardUser($sf_guard_user)
  {
    parent::savesfGuardUser($sf_guard_user);
    $user_id = $sf_guard_user->getId();
    $c = new Criteria();
    $c->add(sfGuardUserProfilePeer::USER_ID,$user_id);
    $profile = sfGuardUserProfilePeer::doSelectOne($c);
    if(!$profile) {
      $profile = new sfGuardUserProfile();
      $profile->setUserId($user_id);
      $profile->save();
    }
  }
}
 
by Roberto Carvajal on 2007-10-30, tagged profile  sfguard  user 

Comments on this snippet

gravatar icon
#1 will killian on 2007-10-30 at 02:52

Rather than directly modify the plugin, you can create an sfGuardAuth module in your application and modify that. You just have to be sure that the action class in your module extends BasesfGuardUserActions, and that include a require_once statement with the path to BasesfGuardUserActions in your modules action class.

Overriding the plugin module with your own module will keep you from having to patch newer versions of the plugin.

gravatar icon
#2 Roberto Carvajal on 2007-10-30 at 08:37

That's correct and that's what I wanted to do in the first place but couldn't get that to work, and reading your comment I realized I didn't added the require_once to BasesfGuardUserActions, I'll try it and if it works for me, I'll update the snippet..

Thank you!

gravatar icon
#3 Roberto Carvajal on 2007-10-30 at 08:49

That worked like a charm, I updated the snippet, thanks again!.

r.

gravatar icon
#4 will killian on 2007-11-06 at 04:08

No problem. Glad I could help.

gravatar icon
#5 Guglielmo Celata on 2008-05-07 at 01:27

Hi, Roberto. Rather than using the sfGuardUserProfile class, I think you should use the proxy getProfile() method of the sfGuardUser class.<br/>

If you refactorize this way, you can use whichever class you want to store user profile information (you just need to declare it in the app.yml configuration file). <br/>

Besides, the code is a lot cleaner, since the getProfile() proxy method already check the existance of a profile record and in case create a new instance.<br/>

I use this code, and it works smoothly.

<pre> require_once(sfConfig::get('sf_plugins_dir').'/sfGuardPlugin/modules/sfGuardUser/lib/BasesfGuardUserActions.class.php');

class sfGuardUserActions extends BasesfGuardUserActions { protected function savesfGuardUser($sf_guard_user) { parent::savesfGuardUser($sf_guard_user); $profile = $sf_guard_user->getProfile(); if($profile->isNew()) { $profile->setUserId($sf_guard_user->getId()); $profile->save(); } } }

</pre>

Thanks for the snippets, because that's where I started from.

You need to create an account or log in to post a comment or rate this snippet.