I am having some issues with defining relationships between models and having them save correctly. I can't get anything more complicated than a ...->HasOne(...) relationship to work.
Consider this for example:
A user is logged in and wants to create a new role. We give the role a title and hit 'submit'. Upon submittion, I need to inser this new role into the data base and automatically add the current user to the role.
I am not sure what relationships I need to define in my models (User/UserRole/Role) and how I would go about actually inserting items into the data base etc.
I tried to define a many to many relationship from the User to UserRole to Role, something like this:
//user
$this->hasManyToMany( "Id", "UserRole", "UserID", "RoleID", "Role", "Id" );
I did the opposite in Role model
//Role
$this->hasManyToMany( "Id", "UserRole", "RoleID", "UserID", "User", "Id" );
And finally, in the UserRole model, I created 2 'belongs to' relationships like so:
//UserRole
$this->belongsTo("UserID", "User", "Id");
$this->belongsTo("RoleID", "Role", "Id");
But thats all just starting to get confusing and I am not sure what I am doing wrong or right. For example, I tried the following to create a new role and add the active user to it immediately:
$newRole = new Role();
$newRole->name = "Test Role";
$newRole->UserRole = array();
$newRole->UserRole[0] = new UserRole();
$newRole->UserRole[0]->RoleID = $newRole->Id;
$newRole->UserRole[0]->UserID = $activeUserID;
$newRole->create();
And that just isn't working. I am just a little lost and was wondering if someone can point me in the right direction!