模型内部的模型更改导致CodeIgniter

时间:2011-12-04 02:57:50

标签: php codeigniter

我正在尝试为我的CI模型进行多对多的db调用,并且这样做我在模型中调用模型...如果有更好的方法我就是全部耳朵!

我有三种模式:

Models:
- events
- children
- eventChildren

他们进行基本的CRUD操作。在我的events模型中,我有GetEvents方法接受withChildren参数。如果传递了withChildren参数,我想让孩子们与事件相关联。

当我从eventChildren模型中调用events模型时,结果会变得有趣。我已将其缩小到$this->db->get('eventChildren');模型内的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 Model(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();
    }
}

概要

我的问题出现在event_model文件中,我在完成初始调用之前调用了外部模型,从而覆盖了主DB调用。我不得不将代码移到$query = $this->db->get('events');下方,一切正常。

非常感谢@landons帮助我完成这项工作。

2 个答案:

答案 0 :(得分:2)

我个人认为从其他模型加载模型没有问题。这实际上是保持共享逻辑分离的好方法。因此,保持干燥(不要重复自己)有时需要这种方法。

我对你在第二个输出示例中输出两个数组这一事实感到有点困惑(你的代码只有一个print_r()调用)。子加载函数是否有print_r()?它是否再次调用相同的功能?你的$ this-&gt; db-&gt; get('eventChildren')调用是返回result()还是first_row()?

如果没有看到你的eventChildren模型的GetEventChildren()函数,我无法帮助更多......

答案 1 :(得分:0)

有些人肯定会打另一个电话,就像兰登说的那样。此外,变量名称重叠的可能性很高,因为第二个示例中的两个print_r都没有返回与第一个相似的结果。