2012-03-11 48 views
1

我的CodeIgniter模型方法抓住所有的「案件」與一個特定的子類別。然後假設將它們轉換爲具有適當「get_case」結果的新數組,但它會一直返回相同的數組元素。我調試的每個關聯方法似乎都能正常運行,但在此函數中我得到了相同的結果。CodeIgniter模型數組不按預期

這裏是我遇到問題的方法/函數...這似乎應該是簡單

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

回答

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

就是這樣,感謝 – jnollette 2012-03-11 08:59:19

+0

這是偉大的 - 很高興它幫助。如果您對回覆感到滿意,您會介意將其標記爲已接受的答案嗎?它有助於保持SO組織,並作爲回報,我們都獲得了更好的聲譽:) – 2012-03-11 09:14:08