Hi
Is it possible to set up a relationship between objects without saving those objects?
Say I have users and groups as models with a one-to-many relationship (each user belongs to one group).
If I want to create a new user, I can set the various properties on the $user object first, and then save later.
eg. I can go:
$user->firstname = 'Joe';
$user->surname = 'Bloggs';etc. which sets the values on the model, but doesn’t save it. I can then pass this object around to various functions, in different scopes, to set more of the properties.
When I’m finally happy with the state of the model, I can save it to the database with
$user->save();
If however, I want to relate the user to a group, I have to save it right away:
$user->save($group);If possible, I’d like to just set up the relationship in the user object, doing something like this:
$user->group = $groupwithout saving, and then continue to modify the $user properties:
$user->firstname = 'John';
$user->surname = 'Doe';until finally saving both the $user and its relationship to the group with
$user->save();
The reason I’m looking for this functionality is, I’m setting different user properties and relationships in different places in the code. Some properties, and some relationships, are set in controller functions, and some are set in model methods. For db performance reasons, I dont want to have to save the same user object every time I change scope.
I tried doing the $user->group = $group thing, but it didn’t pass the validation (group is required on my user model). I also tried setting the $user->group_id but that didn’t pass validation either.
Do you have any suggestions?
cheers,
Colm