jacobnollette
jacobnollette

Reputation: 524

CodeIgniter Model array not pushing as expected

My CodeIgniter model method grabs all of the "cases" with a specific subcategory. Then it's suppose to translate those into a new array with proper "get_case" results but it keeps returning the same array element. Every associated method I debug seems to be functioning properly on its own, but in this function I get the same result repeated.

here is the method/function I am having trouble with... This seems like it should be simple

function get_subcategories_cases($subcategory)
    {
    $this->db->order_by('sortorder','asc');
    $this->db->where('subcategoryid', $subcategory);
    $query = $this->db->get('cases');
    $query = $query->result();
    //
    $outputArray = array();
    foreach ($query as $item) {
    array_push($outputArray, $this->get_case($item->id));
    }
    return $outputArray;
    }

here is the get_case method

function get_case($id)
{
//print_r($id);
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get('cases');
$query = $query->row();
//
$this->id               = $query->id;
$this->sortoder         = $query->sortorder;
$this->surgeonid        = $query->surgeonid;
$this->subcategoryid    = $query->subcategoryid;
$this->age              = $query->age;
$this->gender           = $query->gender;
$this->typeofsurgery    = $query->typeofsurgery;
$this->isactive = $query->isactive;
$this->isfeatured       = $query->isfeatured;
$this->iswarning        = $query->iswarning;
$this->metatitle        = $query->metatitle;
$this->metadescription  = $query->metadescription;
$this->metakeywords     = $query->metakeywords;
$this->casedescription  = $query->casedescription;
$this->photos       = $this->insert_case_photos($query->photos);
//
return $this;
}

Upvotes: 1

Views: 519

Answers (1)

Dan Murfitt
Dan Murfitt

Reputation: 1039

When you are saving the results in get_case() to $this, you are saving them to the model (object) attributes, which have scope outside of the get_case() method, so it maybe something to do with that.

Try saving the query result to a local variable:

function get_case($id)
{
    //print_r($id);
    $this->db->where('id', $id);
    $this->db->limit(1);
    $query = $this->db->get('cases');
    $case = $query->row();
    //
    $case->photos = $case->insert_case_photos($case->photos);
    //
    return $case;
}

Upvotes: 2

Related Questions