| 
	
 | 
 Posted by Toby A Inkster on 02/19/07 17:51 
rpsetzer wrote: 
 
> Yet, I am very concerned about performance. For example, there are lots 
> of cases when I may just be needing the employee name. 
 
The approach I'm using in a current project is along these lines: 
 
	public function load_from_database($keys) {...} 
	public function load_from_array($vals) {...} 
 
Such that the class can be populated from either the database, or from an 
array. It can be partially populated, so that, for example, you can load 
the Employee ID and Employee Name from an array like so: 
 
	$e = new Employee(); 
	$e->load_from_array(array('id'=>1, 'name'=>'Joe')); 
 
where the other fields like address, telephone number, etc would remain 
blank within the object, but if they were asked for: 
 
	print $e->get('phone'); 
 
then the Employee object is smart enough to grab all the missing data from 
the database and return the appropriate value. 
 
So for example: 
 
	$db = new PDO(...); 
	foreach ($db->query('SELECT id, name FROM employees') as $row) 
	{ 
		$e = new Employee(); 
		$e->load_from_array($row); 
		print $e->get('name'); 
		if ($e->get('name')=='Joe') 
			print ' '.$e->get('phone'); 
		print "\n"; 
	} 
 
will work as expected, printing a list of employees, including phone 
numbers for employees called Joe. Yes, that's right -- even when the 
initial query didn't select phone numbers! 
 
When you perform the get('phone') call, $e realises that 'phone' is NULL, 
so performs its own call to load_from_database, which runs 'SELECT * FROM 
employees WHERE id={$this->id}' and then feeds the results into 
load_from_array. 
 
--  
Toby A Inkster BSc (Hons) ARCS 
Contact Me ~ http://tobyinkster.co.uk/contact 
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux 
 
* = I'm getting there!
 
  
Navigation:
[Reply to this message] 
 |