我想做一個多對多的數據庫調用我的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幫助我完成這一切。
我試圖避免發佈整個模型,以保持短小的問題,但據說我使用下面的例子http://heybigname.com/2009/08/28/how-to-write-a -better-model-in-code-igniter /我確實有兩個'$ this-> db-> get()'調用,但它們都在每個模型中。我會用兩種模型更新我的問題。 – Seth
我已經添加了模型。 – Seth
靠近。現在,你傳遞了什麼參數,以及你期望的輸出是什麼? – landons