I'm working on my own PHP framework for a long while now.Right now I'm in a refactoring process.I'm came up with the question what a programmer would excpect what happens when he see following code:// working object orientated with a database.$UserGroup = new UserGroupModel();$UserGroup->Title = 'Guest';$UserGroup->Insert(); // execute sql$User = new UserModel();$User->Name = 'Peter';$User->UserGroupID = $UserGroup->UserGroupID;// print all usersprint_r($UserGroup->Users);$User->Insert();  // execute sql// print all usersprint_r($UserGroup->Users);Would you expect that $UserGroup->Users imeditaley contains the reference or after the  statement $User->Insert();.(Currently it is the second one.)The same with an update statement:// lets pretend we got the $UserGroup1 and $UserGroup2 from the database$User = $UserGroup1->Users[0];$User->UserGroupID = $UserGroup2->UserGroupID;print_r($UserGroup1->Users);print_r($UserGroup2->Users);$User->Update();print_r($UserGroup1->Users);print_r($UserGroup2->Users);Would you expect that $UserGroup->Users imeditaley contains the changed reference or after the  statement $User->Update().(Currently it is the first one.)Because working with new Objects and already existing ones behave differently I thought it might could be confusing.My suggestion would be that the reference is updated imeditaley.At the moment references for new Objects aren't updated imeditaley because the reference is based on the primary key that doesn't exists yet. It exists after we run Insert().Maybe I could solve this problem by working internal ids and not database ids.But the real question is, what would you expect what happens (for the references) without reading any documentation?EDIT@showerheadIf I got you right, you would consider this as an better way:$UserGroup = new UserGroupModel();$UserGroup->Name = ...;$UserGroup->Save(); // runs an insert or update sql statement$User = new UserModel();$User->Name = ...;$User->UserGroupID = $UserGroup->UserGroupID;$User->Save(); // runs an insert or update sql statementprint_r($UserGroup->Users); // would be still empty here$UserGroup->LoadChanges(); // load the changes// now we have the new reference in $UserGroup->Usersprint_r($UserGroup->Users);
Code behaviour: Working object orientated with a database
php