![]() |
|
Snippets |
|
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(); } } }
Comments on this snippet
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.
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!
That worked like a charm, I updated the snippet, thanks again!.
r.
No problem. Glad I could help.
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.