简体   繁体   English

优化将参数从控制器传递到模型以进行mysql查询的方式

[英]optimize way of passing paramters from controller to model for mysql query

I am working on a project in condeigniter(newbie). 我正在condeigniter(newbie)中从事一个项目。 My project include listing of products using different filters like categories, subcategories, keywords, minimum-maximum prices etc. I have done this hundred of times in custom php, where i make variable $where for all conditions in where clause. 我的项目包括使用类别,子类别,关键字,最低-最高价格等不同过滤器列出产品。我已经在自定义php中完成了一百次,在其中我为where子句$where的所有条件设置了$where变量。 For example 例如

$where = '';
if($category != '')
{
  $where .= " category = '$category'";
}

Now in codeigniter I have multiple paramters,which I can get in my controller through uri segment and call model function, pass all parameters and run mysql query there. 现在在codeigniter中,我有多个参数,可以通过uri segment进入controller并调用model函数,传递所有参数并在其中run mysql query The problem is I am checking every parameter if it is empty or set and run multiple queries accordingly. 问题是我正在检查每个参数是否为空或已设置,并相应地运行多个查询。 For example. 例如。

if($category != '' && $subCategory != '')
{
  //whole query here
}
else  if($category == '' && $subCategory != '')
{
  //whole query here
}

//and so on

What I need is any optimize way of doing this, as I have joins (multiple paramters). 我需要的是执行此操作的任何优化方法,因为有联接(多个参数)。

My sample conditions and queries 我的样本条件和查询

if($subCat == '' && $vendor == '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->where('categories.name',$cat);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else if($subCat == '' && $vendor != '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('vendors','vendors.id = listings.programme');
        $this->db->where('categories.name',$cat);
        $this->db->where('vendors.name',$vendor);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else if($subCat != '' && $vendor == '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('subcategories','subcategories.id = listings.subcategory');
        $this->db->where('categories.name',$cat);
        $this->db->where('subcategories.name',$subCat);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $subCat = str_replace("-"," ",str_replace("-and-"," & ",$subCat));
        $vendor = str_replace("-"," ",str_replace("-and-"," & ",$vendor));

        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('subcategories','subcategories.id = listings.subcategory');
        $this->db->join('vendors','vendors.id = listings.programme');
        $this->db->where('categories.name',$cat);
        $this->db->where('subcategories.name',$subCat);
        $this->db->where('vendors.name',$vendor);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();  
        return $query->result();
    }

You need to write the query once only and not every time. 您只需要一次而不是每次都编写查询。 For example you may have several conditions as you have described. 例如,您可能已经描述了几种情况。 For example, here is something what I have done: 例如,这是我所做的事情:

$this->db->select('resource.id,city.name as city_name,city.provinceID,resource.Rate, skills.name')->from('resource')
->join('city','resource.cityID = city.id')
->join('resourceToSkillsMap','resource.id = resourceToSkillsMap.resourceID')
->join('skills','skills.id =  resourceToSkillsMap.skillsID');

if($category != '' && $subCategory != '') #condition
{
    $this->db->where('condition');
}
else  if($category == '' && $subCategory != '') #condition
{
    $this->db->where('condition');
}

$rs = $this->db->group_by('resource.id')
->limit($limit,$offset)
->get();

See how the query is written only once but the where condition is given inside the if else . 看看查询只写only onceif else内部给出where condition SO your query will be build up that way. 因此,您的查询将以这种方式建立。 After the query is executed just echo $this->db->last_query(); 执行查询后,只需echo $this->db->last_query(); to echo the query and adjust it as you want it. 回显查询并根据需要进行调整。

In your helper you can construct your $where array() like this 在您的助手中,您可以像这样构造$where array()

$where = array();
if($category != '')
{
   $where['category'] = $category;
} else {
   $where['someotherkey'] = 'someothervalue';
}

And in your controller you just pass the $where array while calling the model. 在控制器中,您只需在调用模型时传递$where数组即可。

$this->foo_model->foo_method($where);

And in model you can do something like this 在模型中,您可以执行以下操作

$this->db->where($where);

EDIT 编辑

if($category != '')
{
   //Along with your $this->db->where()
   $this->db->join('table1','condition1');
} else {
   //Along with your $this->db->where()
   $this->db->join('table2','condition2');
}

To Tidy Up things 整理东西

$cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
$this->db->select('listings.* ');
$this->db->from('listings');
$this->db->join('categories','categories.id = listings.category');  
$this->db->where('categories.name',$cat);

if($vendor != '')      {
  $this->db->join('vendors','vendors.id = listings.programme');
  $this->db->where('vendors.name',$vendor);
}
if($subCat != ''){
  $subCat = str_replace("-"," ",str_replace("-and-"," & ",$subCat));
  $this->db->join('subcategories','subcategories.id = listings.subcategory');
  $this->db->where('subcategories.name',$subCat);
}    

$this->db->limit($perpage,$page);
$query = $this->db->get();
return $query->result();

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

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