Adding to the sfDoctrineGuardPlugin

Here are some excellent resources for working with sfDoctrineGuardPlugin:

Plugin page on symfony (README is possibly out of date)
http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin

Tutorial on how to add a profile to work with sfDoctrineGuardPlugin
http://www.symfony-project.org/blog/2008/11/12/call-the-expert-customizing-sfdoctrineguardplugin

Tutorial on how to add a registration module
http://www.symfony-project.org/blog/2008/12/03/call-the-expert-simple-registration-with-sfdoctrineguardplugin

First of all, the schema code in the documentation for sfDoctrineGuard appears to be old:

(be mindful of the lack of proper YAML spacing; I haven’t figured out how to incorporate code into wordpress.com posts yet)

sf_guard_user_profile:
_attributes: { phpName: sfGuardUserProfile }
id:
user_id:     { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade }
first_name:  varchar(20)
last_name:   varchar(20)
birthday:    date

I replaced it with:

sfGuardUserProfile:
columns:
user_id:     { type: integer, notnull: true, required: true }
first_name:  string(20)
last_name:   string(20)
email:       string(75)
relations:
sfGuardUser: { local: user_id, foreign: id, onDelete: CASCADE }

This schema could be parsed by Doctrine in Symfony 1.2. I ran the symfony doctrine:build-all-reload task again. However it coughed up an error from MySQL:


SQLSTATE[HY000]: General error: 1005 Can't create table 'register.#sql-ae0_25'
(errno: 150). Failing Query: ALTER TABLE sf_guard_user_profile ADD FOREIGN KEY
(user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE

This seems to be a problem with the field definition. It has to do with user_id being integer (bigint in the SQL), and the id field in the plugin’s schema set to integer(4). Others have run into this error:

http://forums.mysql.com/read.php?22,19755,19755#msg-19755

Anyhow, I changed the definition for user_id to be the primary key. This is how it should be anyway; each user should only have one profile.

sfGuardUserProfile:
columns:
user_id:     { type: integer(4), primary: true }
...etc...

I ran symfony doctrine:build-all-reload again, and everything was fine…

Until I started using fixtures to load some default users and profiles:

sfGuardUser:
sgu_admin:
username:       admin
password:       mr9o1ob7
is_super_admin: true
normal_user:
username:       user
password:       password
is_super_admin: false</code>

sfGuardUserProfile:
sgup_admin:
sfGuardUser:    sgu_admin
first_name:     Jonathan
last_name:      Montgomery
email:          jjmontgo@gmail.com
sgup_user:
sfGuardUser:    normal_user
first_name:     Normal
last_name:      User
email:          noone@gmail.com

sfGuardPermission:
sgp_admin:
name:           admin
description:    Administrator permission

sfGuardGroup:
sgg_admin:
name:           admin
description:    Administrator group

sfGuardGroupPermission:
sggp_admin:
sfGuardGroup:       sgg_admin
sfGuardPermission:  sgp_admin

sfGuardUserGroup:
sgug_admin:
sfGuardGroup:       sgg_admin
sfGuardUser:        sgu_admin

This loaded to the database with user_id set to 0 in the user profile.

I tinkered around a bit more, altered the sfGuardUser relation under the profile schema (which didn’t seem to help), and eventually removed the primary key from user_id.

That worked. I’m not sure why. I don’t see why sfGuardUserProfile should have its own id field as a primary key. Please comment on this if you know the answer.

Here is my final sfGuardUserProfile schema:

sfGuardUserProfile:
actAs: [Timestampable]
columns:
user_id:
type: integer(4)
first_name:  string(20)
last_name:   string(20)
email:       string(75)
relations:
sfGuardUser:
local:     user_id
foreign:   id
type:      one
foreignType: one

2 Comments

  1. TLJ
    Posted July 12, 2009 at 9:51 am | Permalink

    The MySQL error is caused by having different types on the columns you are using for the foreign key.

    In the sfGuardUser doctrine schema, the user_id is integer(4) which is a mysql integer. In your doctrine schema the user_id is integer, which is a mysql bigint.

    If you change the profile user_id to integer(4) the relation should work.

    • jjmontgo
      Posted July 12, 2009 at 11:26 am | Permalink

      Thank you TLJ! I noticed this much later on while reviewing the plugin’s schema.yml.


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*