Saturday, August 27, 2011

CakePHP 2 - HABTM saving issue

I have a 'users' table, an 'auth_sources' table, and an 'auth_sources_users' table to join them.

I added an extra field, 'unique_value' to the auth_sources_users table which stores a unique value to identify that user against the chosen auth source.

In my User model, I have this:

var $hasMany = array(
'AuthSourcesUser'
);

In my AuthSource model I have this:

public $hasMany = array(
'AuthSourcesUser'
);

In my AuthSourcesUser model I have this:

public $belongsTo = array(
'AuthSource' => array(
'className' => 'AuthSource',
'foreignKey' => 'auth_source_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

But Cake isn't treating this anything like a HABTM relationship...

I have a normal HABTM relationship relating users and roles, (without a join model manually being created) and that one works fine.

In my /users/add form, I create hidden fields populated with the data for AuthSourcesUser, and after the form submits I manually add the Role id (for security).

Here's what the array looks like which gets submitted:

Array (     [User] => Array         (             [email] => (my email here)             [first_name] => Ben             [last_name] => McClure         )      [AuthSourcesUser] => Array         (             [auth_source_id] => 1             [unique_value] => (my unique value here)         )      [Role] => Array         (             [id] => 2         )  )


When I use saveAll($this->request->data) I get something weird, though:

The User record is created properly, the RolesUser record gets created properly, but the AuthSourcesUser record does not. I get two records created in the auth_sources_users table, both having auth_source_id, user_id, and unique_value all empty.

Then, when I delete the User record, the associated record is deleted from roles_users, but neither record is deleted from auth_sources_users.

What am I doing wrong?

Thanks,

Ben

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: