Welcome Guest, Not a member yet? Register   Sign In
Help needed for multiple model instance.
#1

[eluser]Unknown[/eluser]
Hi I'm learning code igniter and as test project, working on resume management site.

In my database there is a user table and resume table. A user may have one or more resumes.

I'd created a user model and resume model to handle database. Better say class but I'm maintaining class in models.

However since user may have more that one resumes, I wanted to one private variable 'resumes' which must be an array to hold instance of resume model. My code is as follow:

Code:
public function setResumes(){
        $this->db->where('user_id ',$this->userId);
        $this->db->select('id,contact_fname,contact_lname');
        $query = $this->db->get('resume');
        $result =  $query->result();
        $CI =& get_instance();
        for($i=0;$i<count($result);$i++){
            
              $CI->load->model('resume');
            $CI->resume->getShortResumeById($result[$i]);
            array_push($this->resume,$CI->resume);
            echo "<br/>".$result[$i]->id."<br/>";
            print_r($this->resume);
        }
        //print_r($this->resume);
    }

Problem, I'm not getting two different instance of resume model. With two rows in DB, first loop cycle's print_r statement show correct row but during second loop cycle, both record gets replaced with second row. here is output.

Code:
1
Array ( [0] => Resume Object ( [id:private] => 1 [contact_title:private] => [contact_fname:private] => rahul [contact_lname:private] => bhatt [contact_phone:private] => [contact_country:private] => [contact_state:private] => [contact_zip:private] => [contact_city:private] => [contact_address:private] => [obj_title:private] => [obj_position:private] => [obj_objective:private] => [ref_name:private] => ) )
2
Array ( [0] => Resume Object ( [id:private] => 2 [contact_title:private] => [contact_fname:private] => shdgshdgsdgsh [contact_lname:private] => dsdsdsd [contact_phone:private] => [contact_country:private] => [contact_state:private] => [contact_zip:private] => [contact_city:private] => [contact_address:private] => [obj_title:private] => [obj_position:private] => [obj_objective:private] => [ref_name:private] => ) [1] => Resume Object ( [id:private] => 2 [contact_title:private] => [contact_fname:private] => shdgshdgsdgsh [contact_lname:private] => dsdsdsd [contact_phone:private] => [contact_country:private] => [contact_state:private] => [contact_zip:private] => [contact_city:private] => [contact_address:private] => [obj_title:private] => [obj_position:private] => [obj_objective:private] => [ref_name:private] => ) ) Array ( [0] => Resume Object ( [id:private] => 2 [contact_title:private] => [contact_fname:private] => shdgshdgsdgsh [contact_lname:private] => dsdsdsd [contact_phone:private] => [contact_country:private] => [contact_state:private] => [contact_zip:private] => [contact_city:private] => [contact_address:private] => [obj_title:private] => [obj_position:private] => [obj_objective:private] => [ref_name:private] => ) [1] => Resume Object ( [id:private] => 2 [contact_title:private] => [contact_fname:private] => shdgshdgsdgsh [contact_lname:private] => dsdsdsd [contact_phone:private] => [contact_country:private] => [contact_state:private] => [contact_zip:private] => [contact_city:private] => [contact_address:private] => [obj_title:private] => [obj_position:private] => [obj_objective:private] => [ref_name:private] => ) )

What is wrong with it and most important, what is right way of handling following requirements:

1. Users may have more than one resume.
2. Resume table is complicated so will be broken in many tables. Below is DB script if it helps to suggest best way of handling it.

Can't post DB SQL due to size limit Sad




Theme © iAndrew 2016 - Forum software by © MyBB