尝试使用Active Record在codeigniter中整合我的查询...这是更好的方法

时间:2013-11-28 08:00:20

标签: php codeigniter

我有一个codeigniter应用程序,我的模型将当前月份的一些统计信息发送到前端。我的代码现在似乎非常冗余,但我不知道如何使用Active Record查询进行组合。希望有人能告诉我下面的简化版本。最初,我得到了我们所在的月份(从今天开始)。以下查询之间的唯一区别是$ this-> db-> where('leadStatus',0)。 4个查询可以以某种方式组合吗?

首先得到所有 下一个获得当前(0) 下一个存档(1) 最后导致了交易(2)

    function get_stats($data) {
date_default_timezone_set('America/Halifax');
$today = date("Y-m-t 23:59:59") ;
$monthStart = date('Y-m-01 00:00:00');
$parent_id = $data['parent_id'];


//leads all
$this->db->select('id');
$this->db->from('leads');
$this->db->where('parent_id', $parent_id);
$this->db->where('leadCreated >=', $monthStart);
$this->db->where('leadCreated <=', $today);

$query = $this->db->get();
$data['total_leads'] = $query->num_rows();


//leads current
$this->db->select('id');
$this->db->from('leads');
$this->db->where('parent_id', $parent_id);
$this->db->where('leadStatus', 0);
$this->db->where('leadCreated >=', $monthStart);
$this->db->where('leadCreated <=', $today);

$query = $this->db->get();
$data['leads_current'] = $query->num_rows();

//leads archived
$this->db->select('id');
$this->db->from('leads');
$this->db->where('parent_id', $parent_id);
$this->db->where('leadStatus', 1);
$this->db->where('leadCreated >=', $monthStart);
$this->db->where('leadCreated <=', $today);

$query = $this->db->get();
$data['leads_archived'] = $query->num_rows();


//leads sent to deal
$this->db->select('id');
$this->db->from('leads');
$this->db->where('parent_id', $parent_id);
$this->db->where('leadStatus', 2);
$this->db->where('leadCreated >=', $monthStart);
$this->db->where('leadCreated <=', $today);

$query = $this->db->get();
$data['leads_todeal'] = $query->num_rows();

2 个答案:

答案 0 :(得分:0)

这就是我在PHP中的表现:

function get_stats($data) {
    date_default_timezone_set('America/Halifax');
    $today = date("Y-m-t 23:59:59") ;
    $monthStart = date('Y-m-01 00:00:00');
    $parent_id = $data['parent_id'];

    $data = array(
        'total_leads' => $this->getLeadsCount($parent_id, $monthStart, $today),
        'leads_current' => $this->getLeadsCount($parent_id, $monthStart, $today, 0),
        'leads_archived' => $this->getLeadsCount($parent_id, $monthStart, $today, 1),
        'leads_todeal' =>  $this->getLeadsCount($parent_id, $monthStart, $today, 2),
    );
}

function getLeadsCount($parent_id, $monthStart, $today, $leads = false) {
    $this->db->select('id');
    $this->db->from('leads');
    $this->db->where('parent_id', $parent_id);
    $this->db->where('leadCreated >=', $monthStart);
    $this->db->where('leadCreated <=', $today);

    if ($leads !== false) {
        $this->db->where('leadStatus', $leads);
    }

    return $this->db->get()->num_rows();
}

但是,我会通过单个SQL查询再做一步:

function getLeadsCount($parent_id, $monthStart, $today) {
    $this->db->select('COUNT(*) AS count', false);
    $this->db->select('leadStatus');
    $this->db->from('leads');
    $this->db->where('parent_id', $parent_id);
    $this->db->where('leadCreated >=', $monthStart);
    $this->db->where('leadCreated <=', $today);
    $this->db->group_by('leadStatus');

    return $this->db->get()->result_array();
}

// Returns an array of arrays, each array containing 'leadStatus' and 'count' keys.
// Note that this won't return total leads - you can combine all the values to get it, though.
$this->getLeadsCount();

您甚至可以使用纯SQL获取总计数:

function getLeadsCount($parent_id, $monthStart, $today) {
    $sql = "SELECT leadStatus, COUNT(*) AS count
            FROM `leads`
            WHERE parent_id = ?
            AND leadCreated >= ?
            AND leadCreated <= ?
            GROUP BY leadStatus WITH ROLLUP";

    // WITH ROLLUP returns an extra row with the total count, but with NULL for leadStatus.
    return $this->db->query($sql, array($parent_id, $monthStart, $today))->result_array();
}

答案 1 :(得分:0)

大免责声明 - 没有测试过这个!但我认为它应该有效

// make an array to hold your common where search values 
$leadsearch = array('parent_id' => $parent_id, 'leadCreated >=' => $monthStart, 'leadCreated <=' => $today);


// now "chain" it together to make it more compact
//leads all
$this->db->select('id')->from('leads')->where($leadsearch) ;
$query = $this->db->get();
$data['total_leads'] = $query->num_rows();

// add the extra where condition of lead status    
//leads archived
$this->db->select('id')->from('leads')->where($leadsearch)->where('leadStatus', 1) ;
$query = $this->db->get();
$data['leads_archived'] = $query->num_rows();

有关详细信息,请参阅手册中的此页面  http://ellislab.com/codeigniter/user-guide/database/active_record.html