2011-12-04 34 views
0

我想做一個多對多的數據庫調用我的CI模型,並做到這一點我從模型內調用模型...如果有更好的方法我'所有的耳朵!模型內部模型更改結果CodeIgniter

我有三個型號:

Models: 
- events 
- children 
- eventChildren 

他們這樣做基本的CRUD操作。在我的events模型中,我有一個GetEvents方法,它接受withChildren參數。如果withChildren參數已通過,我希望獲取與該事件關聯的子項。

當我從events模型的內部調用eventChildren模型時,結果會變得有趣。我已經將其範圍縮小到eventChildren模型內的$this->db->get('eventChildren');呼叫。當這個呼叫突然發生時,我會返回多個事件而不是單個事件。

代碼:

if(isset($options['eventId']) && isset($options['withChildren'])) 
{ 
    // Get ID's of children that are tied to the event. 
    $this->load->model('eventChildren_model', 'eventChildren'); 
    $this->load->model('children_model', 'children'); 

    $eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId'])); 

    // Loop over those ID's and get the children from the children table 
    $children = array(); 

    foreach($eventChildren as $group) 
    { 
     $child = $this->children->GetChildren(array('childId' => $group->childId)); 
     array_push($children, $child); 
    } 

    echo "<pre>"; 
    print_r($children); 
    echo "</pre>"; 
} 

如果我註釋掉這行代碼我返回一個單一事件。如果我取消註釋這一行代碼,則返回所有事件。

$eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId'])); 

這與調用$this->db->get('eventChildren');有關。一旦這個呼叫發生,事件就會變得怪異。

示例返回未做外部模型電話:

Array 
(
    [0] => stdClass Object 
     (
      [eventId] => 2 
      ... 
     ) 

) 

示例返回調用外部模型時:

Array 
(
    [0] => stdClass Object 
     (
      [childId] => 8 
      ... 
     ) 

    [1] => stdClass Object 
     (
      [childId] => 10 
      ... 
     ) 
) 
Array 
(
    [0] => stdClass Object 
     (
      [eventId] => 1 
      ... 
     ) 

    [1] => stdClass Object 
     (
      [eventId] => 2 
      ... 
     ) 

    [2] => stdClass Object 
     (
      [eventId] => 3 
      ... 
     ) 
) 

對不起,所有的代碼。我的最終目標是創建一個多對多的對象,將兒童添加到事件中,以便在我的視圖中輕鬆顯示此對象。我在這裏閱讀的所有內容都不鼓勵從模型中加載模型的做法,但我沒有看到更好的方法。我嘗試了DataMapper和Doctrine,但都無法成功運行。

任何幫助,將不勝感激。

活動模型(GetEvents):(注:_p()是一個輔助功能,僅僅輸出print_r()包裹着<pre>標記)

function GetEvents($options = array()) 
{ 
    // default values 
    $options = _default(array('sortDirection' => 'asc'), $options); 

    // Add where clauses to query 
    $qualificationArray = array('eventId', 'eventStatus'); 
    foreach($qualificationArray as $qualifier) 
    { 
     if(isset($options[$qualifier])) $this->db->where($qualifier, $options[$qualifier]); 
    } 

    // If limit/offset are declared (usually for pagination) then we need to take them into account 
    if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']); 
    else if(isset($options['limit'])) $this->db->limit($options['limit']); 

    // sort 
    if(isset($options['sortBy'])) $this->db->order_by($options['sortBy'], $options['sortDirection']); 

    // add children to the event 
    if(isset($options['eventId']) && isset($options['withChildren'])) 
    {   
     // Get ID's of children that are tied to the event. 
     $this->load->model('eventChildren_model', 'eventChildren'); 
     $this->load->model('children_model', 'children'); 

     $eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId'])); 

     // Loop over those ID's and get the children from the children table 
     $children = array(); 
     foreach($eventChildren as $group) 
     { 
      $child = $this->children->GetChildren(array('childId' => $group->childId)); 
      array_push($children, $child); 
     } 

     echo "Children:\n"; 
     _p($children); 
    } 

    $query = $this->db->get('events'); 
    if($query->num_rows() == 0) return false; 

    _p($query->result()); 

    if(isset($options['eventId'])) 
    { 
     // If we know that we're returning a singular record, then let's just return the object 
     return $query->row(0); 
    } 
    else 
    { 
     // If we could be returning any number of records then we'll need to do so as an array of objects 
     return $query->result(); 
    } 
} 

eventChildren模型(GetEventChildren方法)

function GetEventChildren($options = array()) 
{ 
    // default values 
    $options = _default(array('sortDirection' => 'asc'), $options); 

    // Add where clauses to query 
    $qualificationArray = array('eventChildrenId', 'eventId', 'childId'); 
    foreach($qualificationArray as $qualifier) 
    { 
     if(isset($options[$qualifier])) $this->db->where($qualifier, $options[$qualifier]); 
    } 

    // If limit/offset are declared (usually for pagination) then we need to take them into account 
    if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']); 
    else if(isset($options['limit'])) $this->db->limit($options['limit']); 

    // sort 
    if(isset($options['sortBy'])) $this->db->order_by($options['sortBy'], $options['sortDirection']); 

    $query = $this->db->get('eventChildren'); 
    if($query->num_rows() == 0) return false; 

    if(isset($options['eventChildrenId'])) 
    { 
     // If we know that we're returning a singular record, then let's just return the object 
     return $query->row(0); 
    } 
    else 
    { 
     // If we could be returning any number of records then we'll need to do so as an array of objects 
     return $query->result(); 
    } 
} 

SUMMARY

我的問題是在event_model文件,在完成我的初始調用之前,我正在調用外部模型,因此覆蓋了主DB數據調用。我不得不將該代碼移動到$query = $this->db->get('events');以下,一切正常。

非常感謝@landons幫助我完成這一切。

回答

2

我個人認爲從其他型號加載模型沒有問題。這實際上是保持共享邏輯分離的好方法。所以,保持乾爽(不要重複自己)有時需要這種方法。

我有點困惑,你在第二個輸出示例中輸出兩個數組(你的代碼只有一個print_r()調用)。子載入函數是否有print_r()?它是否再次調用相同的函數?您的$ this-> db-> get('eventChildren')調用是否返回結果()或first_row()?

我不禁沒有看到您的eventChildren模型GetEventChildren()函數更多...

+0

我試圖避免發佈整個模型,以保持短小的問題,但據說我使用下面的例子http://heybigname.com/2009/08/28/how-to-write-a -better-model-in-code-igniter /我確實有兩個'$ this-> db-> get()'調用,但它們都在每個模型中。我會用兩種模型更新我的問題。 – Seth

+0

我已經添加了模型。 – Seth

+1

靠近。現在,你傳遞了什麼參數,以及你期望的輸出是什麼? – landons

0

東西絕對撥打另一個電話,像landons說。此外,由於第二個示例中的兩個print_r都沒有返回與第一個示例類似的結果,因此變量名重疊的概率很高。