Search code examples
symfony1symfony-1.4sfguardsfdoctrineguard

Create an object when saving another


In my database I have the tables : "clients", "userProfile", and "sfGuardUsers"

I want the system to create a profile and a user when I create a new client. Here's what I have so far :

in lib/form/doctrine/ClientForm.class.php :

class ClientForm extends BaseClientForm
{
  public function configure()
  {
    parent::configure();

    $userProfileForm = new UserProfileForm($this->object->UserProfile->user);
    $this->mergeForm($userProfileForm);
  }
}

in modules/client/action/actions.class.php :

class clientActions extends autoClientActions
{
  public function executeCreate(sfWebRequest $request)
  {
    parent::executeCreate($request);
    $this->client->createProfile($request->getParameter('email'),$request->getParameter('password'));
  }

in lib/model/doctrine/Client.class.php :

  public function createProfile($email, $password)
  {
    $profil = Doctrine_Core::getTable('UserProfile')->findOneByClientId($this->getId());
    $profil->createUser($email, $password);
    $profil->setClient($this);
    $profil->save();
  }

According to the log, createProfile(null, null) is called, and mysql creates a user with '' as its username :(


Solution

  • You don't need to do that. Check the readme file. You need this in app.yml:

    sf_guard_plugin:
      profile_class:      sfGuardUserProfile
      profile_field_name: user_id
    

    And, of course, in your db schema, when defining the profile table, you need to also define its relation with sf_guard_user table.

    The plugin will do the rest for you (adding/saving the new profile when need).