0
votes

Since yesterday i'm trying to develop a form in ajax, inserting data to sf_guard_user table and a table called user (here is the profile). The guard table has two collumns "email" and "password" to login, and "user" to retrieve data once logged in.
The registration form is in ajax, and is okey, but i don't know how to insert data in the two tables. I do the following (in an action called 'testing' -> /user/testing).

$user=new sfGuardUser();
$user->email='[email protected]';
$user->password='password';
I do comment the line $user->save(), and I get an error :
Unknown record property / related component "salt" on "sfGuardUser"


Here is the schema for sfGuardUser:
sfGuardUser:
actAs: [Timestampable]
columns:
email_address:
type: string(255)
notnull: true
unique: true
password: string(128)
is_active:
type: boolean
default: 1
is_super_admin:
type: boolean
default: false
last_login:
type: timestamp
(sorry for the bad indexing, don't know how to paste code)



And my other question is.. once I save the form, how do I save data in te "user" table like
$user->user->name='blabla';
$user->user->lastname='blablabla';
etc, etc, etc.

Thank you!!!

2

2 Answers

2
votes

Instead of these lines:

$user=new sfGuardUser();
$user->email='[email protected]';
$user->password='password';

write it like

$user = new sfGuardUser();
$user->setEmail('[email protected]');
$user->setPassword('password');

the difference is that using the public set/get methods is a better design rather then just setting the object parameters. The importance is that right now you are relying on the sfGuardPlugin, which overrides the normal behaviour and setting the password is not just setting the value, it is much more complex than that (generating a salt, getting the hasing algorithm from configuration etc., you can check it out here http://trac.symfony-project.org/browser/plugins/sfDoctrineGuardPlugin/branches/1.3/lib/model/doctrine/PluginsfGuardUser.class.php starting at line 33).

If you call

$user->save();

that will save the data in the sf_guard_user table. To create the profile or in your case the "user" row in the database, create it like th sfGuardUser object (if your "user" table is defined in you schema.yml and the model files are generated)

$profile = new User();
$profile->setName('blabla');
$profile->setLastname('blablabla');

To link the sfGuardUser and the profile objects you need to decide how to implement this 1:1 relationship. For example you can add an sfGuardUserId to the profile:

$profile->setGuardUserId($user->getId());
$profile->save();

If you feel like check it out how I have done the same thing here: http://code.google.com/p/sc2-cup-app/source/browse/trunk/apps/frontend/modules/sfGuardAuth/actions/registerAction.class.php

1
votes

Your schema is wrong, your schema for sfGuardUser should be:

sfGuardUser:
  actAs:
    Timestampable: 
  columns:
    id:
      primary: true
      type: integer
      autoincrement: true
    first_name:
      type: string(255)
    last_name:
      type: string(255)
    email_address:
      unique: true
      type: string(255)
      notnull: true
    username:
      unique: true
      type: string(128)
      notnull: true
    algorithm:
      default: sha1
      type: string(128)
      notnull: true
    salt:
      type: string(128)
    password:
      type: string(128)
    is_active:
      default: 1
      type: boolean
    is_super_admin:
      default: false
      type: boolean
    last_login:
      type: timestamp
    last_action_date:
      type: timestamp

When you create a new user, sfDoctrineGuard salts the password. Hence why you're getting the error of "unknown record property". The property "salt" doesn't exist in your schema. I have a registration form if you would like the code for it.