Chris W. Parker wrote: > On Monday, April 09, 2007 3:51 PM Jochem Maas > <mailto:jochem@xxxxxxxxxxxxx> said: > > Thanks for the response Jochem. > >> Chris W. Parker wrote: > > [snip] > >> you probably only want one DB call to >> populate the User object with all the relevant >> user data at the point where the object is created. > > [snip] > > Ok. I see what you're saying. If I populate all that data during the > constructor why would I ever call the function again right? you could refresh the data if needed - but basically the idea is to cut down the user data grab into a single sql call. > > [snip] > >>> As I started to write this and use it I get the feeling that there >>> should also be an Event class that is extended by the User class. >>> Reason >> if you use an Event class then it should just represent an Event (and >> a User object would [probably] contain an array of Event objects). >> AFAICT there is no good reason to have Event extend User. > > I see. > >>> being that each User object is a reference to the currently logged in >>> user, not anyone else. >> the User class is merely a representation of *a* user - you can >> use an instance for the currently logged in user, but that doesn't >> stop you from using the same class to model the collection of users >> that fall under a given manager. > > I see. > >> // you might need to f around with returning references here, >> // (I can never quite get that right without a bit of trial and error >> in php4) function getEmployees() >> { >> // consider caching the result? >> $emps = array(); >> if ($this->is_manager) { >> >> // get user data from db >> $sql = "SELECT * FROM users WHERE > manager_id={$this->id}"; >> // error checking? >> $db =& DB::singleton(); >> $db->execute($sql); >> while ($data = $db->getRow()) >> $emps[] =& new User($data); $emps[$data['id']] =& new User($data); >> } >> >> return $emps; >> } > > How do I reference a User object within the $emps array? > > Is it like $emps[0]->accrual ? that's one way, you might consider keying the emps array on the user id for easier retrieval (see above), which would allow you to quickly reference the correct employee User object when a manager performs an action on a given emp. or when a manager edits multiple employees: $manager =& new User($_SESSION['userid']); $emps = $manager->getEmployees(); // think about using references here? foreach ($emps as $id => $emp) { if (isset($_POST['emps'][$id])) { // just some vague 'update' concept/action thingummy $emp->doSomeUpdateStuff($_POST['emps'][$id]); $emp->saveUpdateStuffToDB(); } } or a different tack foreach ($_POST['emps'] as $id => $stuff)) { $manager->updateEmpStuff($id, $stuff); } // where updateEmpStuff does something like User { function updateEmpStuff($id, $stuff) { if ($this->is_manager) { // don't forget to cache the emps array?? // don't forget the use of references?? $emps = $this0>getEmployees(); if (isset($emps[$id])) { // again a vague thingummy representing something // a manager might [need to be able to] do. $emps[$id]->managerUpdatesStuff($stuff); } } } } > > > > Thanks, > Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php