CodeIgniter模型数组未按预期推送

时间:2012-03-11 08:25:36

标签: php codeigniter

我的CodeIgniter模型方法使用特定子类别获取所有“案例”。然后它假设将这些转换为具有适当“get_case”结果的新数组,但它不断返回相同的数组元素。我调试的每个相关方法似乎都在自己正常运行,但在这个函数中我得到了相同的重复结果。

这是我遇到问题的方法/功能......这看起来应该很简单

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;
    }

这是get_case方法

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;
}

1 个答案:

答案 0 :(得分:2)

当您将结果保存到get_case()$this时,您将它们保存到模型(对象)属性中,这些属性的范围超出get_case()方法,因此它可能与此有关。

尝试将查询结果保存到本地变量:

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;
}