【问题标题】:how to make common join function in codeigniter for listing如何在codeigniter中创建通用连接函数以进行列表
【发布时间】:2012-12-21 07:41:28
【问题描述】:

我在 codeigniter 中有一个用于列出的连接函数,但它只连接两个表,对于三个表我必须使用另一个函数等等有没有办法让这些函数对任何数量的连接都通用 型号代码

public function __construct()
{
    $this->load->database();
}

//listing with join for two tabels
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
        $this->db->select($value);
            $this->db->join($table2,$condi,$join_type);
    $this->db->order_by($order_by,$order);
    $this->db->where($where);
    return $query= $this->db->get($table, $limit, $offset);
}
//listing with join for three tabels
public function get_joinlist1($table,$value,$table2,$condi1,$join_type1,$table3,$condi2,$join_type2,$where,$order_by,$order)
{
    $this->db->select($value);
    $this->db->join($table2, $condi1,$join_type1);
    $this->db->join($table3, $condi2,$join_type2);
    $this->db->where($where);
    $this->db->order_by($order_by,$order);
    return $this->db->get($table);
}

【问题讨论】:

  • 您可以将连接参数放在一个数组中,然后循环访问它们以在函数中访问它们吗?
  • 你是否需要使用 Active Record 类强制编写查询......
  • 感谢大家@Venkat 的回复实际上不是,但我的模型包含所有以活动内容编写的查询
  • 没问题老兄。我在所有应用程序中都遵循正常的查询规则。

标签: php codeigniter activerecord join


【解决方案1】:

这是一个非常简单的示例,可以帮助您入门

$joins构造为数组:

$joins = array(
    array(
        'table' => 'table2',
        'condition' => 'table2.id = table1.id',
        'jointype' => 'LEFT'
    ),
);

将连接作为数组处理的示例函数:

public function get_joins($table, $columns, $joins)
{
    $this->db->select($columns)->from($table);
    if (is_array($joins) && count($joins) > 0)
    {
        foreach($joins as $k => $v)
        {
            $this->db->join($v['table'], $v['condition'], $v['jointype']);
        }
    }
    return $this->db->get()->result_array();
}

【讨论】:

  • 哦,知道了,我会试试这个,如果有任何问题,我会回复,非常感谢
  • @NishantLad 祝你好运冠军
  • 昨天的分页问题你有什么收获吗?
  • @Dale 非常感谢.. 它帮助我进行了常见的加入查询.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多