I ran into the same problem, I assume you are using MySQL? this is one of the common OOP design issues, since SQL tends to smooth everything.
I solved this by doing the following
1.) create a class that has three instance forms,
where is he new
$myStudent = new $Student();
another where you know the id but need ids data
$myStudent = new $Student($student_id);
and another where you already have data in an associative array
$data = array('id'=13,'name' => 'studentname', 'major' => 'compsci'); $myStudent = new $Student($data['id'], $data);
This allows you to create a factory class that can run a query from mysql, retrieve an associative data array, and then create student instances from this array data without getting into the database for each student instance.
here is the constructor for this class:
public function __construct($id=FALSE, $data=FALSE) { if(!$id) $this->is_new = true; else if($id && !$data) $this->get_data_from_db($id); else if($id && $data) $this->set_data($data); }
Fire crow
source share