简体   繁体   English

在Codeigniter中创建大型动态查询的最佳方法?

[英]Best way to create a large dynamic query in Codeigniter?

The scenario is a reporting application. 该方案是一个报表应用程序。 To date, MVC applications I've worked with typically have model methods that respond to one specific question (ie get_username_from_id($id) ). 迄今为止,MVC应用我已经与通常的工作有一个具体的问题(即响应模型方法get_username_from_id($id) What I'm looking to do is have one query that can respond many different ways depending on checkboxes selected. 我想要做的是有一个查询,可以根据选中的复选框响应许多不同的方式。

So - for example, I may have a query with just one thing in the select or it may have 100 things in the select. 所以-例如,我可能在查询中仅选择一项,或者在查询中选择100项。 The way I've done it, it's gotten messy fast. 我这样做的方式,很快就变得混乱了。 For example, I wind up doing things like this: 例如,我结束了这样的事情:

if(in_array("id", $grain)){
    $sql .= "table.id id, ";
}

Is there a best practice for this kind of thing? 对于这种事情是否有最佳实践? Or even a way to handle the group by so there isn't a bunch more extra code? 甚至是一种处理小组的方式,因此没有更多的额外代码? I know this is kind of open ended, and I'm probably not even phrasing my question correctly. 我知道这是开放式的,我什至没有正确地表达我的问题。 Much appreciation even to folks who can chime in and help me ask the right question. 甚至对能够插话并帮助我提出正确问题的人们也深表感谢。

I use something like this: 我用这样的东西:

function apply_filters ($filters)
{
        if(isset($filters['user_id']) AND !empty($filters['user_id']))
        {
                $this->db->where('user_id', $filters['user_id']);
        }
}
function get ($filters)
{
        $this->apply_filters($filters);
        return $this->db->get('favourites')->result();
}
function update ($filters, $data)
{
        $this->apply_filters($filters);
        $this->db->update('favourites', $data);
}

You should be able to extend the apply_filters function to your needs. 您应该能够根据需要扩展apply_filters函数。 The question is spared alot of details of what you want to do and samples of real data and code, so this is what I can suggest so far. 这个问题不遗余力地提供您想要做什么的详细信息以及真实数据和代码的示例,因此到目前为止,这是我可以建议的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM