Closed raeldc closed 14 years ago
I'll take a look at this tonight. Is your jelly-auth repo up to date with this broken code? I want to test it.
There are a couple issues here.
Once you figure out what you want your primary key for roles to be, setting the roles relation on the user by just passing a loaded model will work.
Kohana's ORM Auth uses the id column as the primary key for roles, so you may want to follow that convention.
Hopefully that all makes sense and let me know that this all works for you (I got it working over here).
542b7fdd3aa6e77628ddf1ec0cdd352bc40f6b70
Thanks Jonathan!
The reason why I made 'name' as my primary key is because I don't want to do this as my code:
$login = Jelly::factory('role', array('name' => 'login'));
$admin = Jelly::factory('role', array('name' => 'admin');
I just want to do a shortcut:
$login = Jelly::factory('role', 'login');
$admin = Jelly::factory('role', 'admin');
Is there a way to do the shortcut without having to set the primary_key as 'name'? In sprig I was able to do this by overriding the unique_key function.
Thanks!
Unfortunately, there isn't any way to do this currently.
This is the same as Issue #10.
I think it deserves a new issue.
This is my code:
` $user = Model::factory('user'); $login = Jelly::select('role')->where('name', '=', 'login')->load(); $admin = Jelly::select('role')->where('name', '=', 'admin')->load();
$user->set(array(
'username' => 'test',
'password' => 'testuser',
'password_confirm' => 'testuser',
'email' => 'testuser@test.com',
'roles' => array($login, $admin),
'logins' => 0,
));
$user->save();
`
This is the error message:
Database_Exception [ 1452 ]: Cannot add or update a child row: a foreign key constraint fails (`kohana3`.`roles_users`, CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE) [ INSERT INTO `roles_users` (`role_id`, `user_id`) VALUES (NULL, 1), (NULL, 2) ]
Incidentally, it is now possible to use any unique key in select(). Override the builder for the model and override the unique_key()
function.
You no longer need name to be your primary key.
I encountered this issue while creating Jelly Auth.
When saving objects with relationship, Jelly doesn't seem to pass the new insert_id. So this code wouldn't work because it passes NULL to user_id of roles_users:
But it will work if I separate the saving of the roles relationship, and save it again.
I tried other variations like using add(), but same problem, I have to save it twice
Also, notice that I'm manually specifying to pass the ID of $login and $admin models. That's because passing the model objects themselves doesn't seem to work.
Is this an issue or I'm just not getting how things should work?
Regards!