3

I'm using sfDoctrineGuardPlugin to manage users, groups and authentication in my Symfony application.

Now I would like to add an avatar to the sfGuardUser model. How can I do that? What's the best approach? I have seen several ways on the Internet, but I do not want to mess my code up.

Thanks!

elitalon
  • 9,191
  • 10
  • 50
  • 86

2 Answers2

2

There are two ways.

  • Extending the sfGuardUser model
  • Create a Profile model 1:1

The second one is the most recommended specially if you are planing to have a lot of information inside your user model. But if you have only an avatar field to add, extending sfGuardUser is more convenient.

To do so, put the new sfGuardUser.schema.yml inside your /config/ folder. It will be used instead of the plugin one's.

To create a "Profile" there is nothing particular to know about:

Profile:
  tableName:                   profile
  columns:
    sf_guard_user_id:          integer
    firstname:                 varchar(100)
    phone:                     varchar(15)
  relations:
    sfGuardUser:               { class: sfGuardUser, onDelete: CASCADE, local: sf_guard_user_id, foreign: id, foreignType: one }
elitalon
  • 9,191
  • 10
  • 50
  • 86
Damien
  • 5,872
  • 2
  • 29
  • 35
  • Extending the model is the preferred option unless you're adding a lot of data that is seldom required when you retrieve the primary object. Otherwise, there's no point in splitting data that is always needed together across two tables. – Jeremy Kauffman Jul 04 '11 at 17:33
2

My preferred approach requires no database changes:

  • Add sfWidgetFormInputFileEditable and sfValidatorFile to sfGuardUserForm. Configure the widget and validator as required by your needs (deleting allowed, image types, sizes, etc.)
  • Add a method to sfGuardUser that generates a unique path based on the immutable properties of the user (e.g. hashing the the user id).
  • Use this method to both render the images and set the upload path for your widget and validator in your form.
Jeremy Kauffman
  • 10,293
  • 5
  • 42
  • 52