如何调用Codeigniters 2 Active记录从同一查询中获取2组结果?

时间:2017-09-29 19:33:47

标签: php mysql codeigniter

我实际上是为文件导出创建了一个分块功能,我有一个select-> from->我已经在那里构建了。我想能够打电话给'得到'每次两次使用不同的限制/偏移值。

这是一个基本想法的演练。

// BallReport.php
function ProcessData(){
    //Report 1
    $query = createSelectQuery();
    $query = applyReportOneWhereValues($query);
    $results1 = CSVTool::processLargeDataSet($query, 10, 1000);

    //Report 2
    $query = createSelectQuery();
    $query = applyReportTwoWhereValues($query);
    $results2 = CSVTool::processLargeDataSet($query, 10, 1000);
}

function createSelectQuery(){
    // the select is complicated having multiple joins and sub queries 
    // so I only want to have to write this once
    $query = $this->db->select('ball.name,
            color.name,
            size.name,
            shape.name')
        ->from('ball')
        ->join('color', 'ball.color_id = color.id')
        ->join('size', 'ball.size_id = size.id')
        ->join('shape', 'ball.shape_id = shape.id');
    return $query;
}

function applyReportOneWhereValues($query){
    // I have 2 different sets of where parameters
    // But they are both using the same select
    // so I separated them into these functions
    // So I can apply the set of where statements
    // all at once
    $query = $query->where("table.color", "blue")
                   ->where("table.size" , "large")
                   ->where("table.shape", "round");
    return $query;
}

function applyReportTwoWhereValues($query){
    $query = $query->where("table.color", "red")
                   ->where("table.size" , "small")
                   ->where("table.shape", "round");
    return $query;
}

//In CSVTool.php 
public static function processLargeDataSet($query, $numberOfPages, $chunkSize){
    // Since the data set is going to be so large we want to process in chunks 
    // So that we don't hit the limit and break mid way. 
    // To do that we only call the DB in sets of 1000 rows
    for(int $i = 0; $i <= $numberOfPages: $i++){
        processRows($query, $i * $chunkSize, $chunkSize);
    }
}

function processRows($query, $offset, $limit){
    // We limit in here so each time it's called we change the offset and limit
    $query = $query->offset($offset)->limit($limit);
    $valuesToProcess = $query->get()->result_array();

    // process the rows here
}

这当然不起作用,因为一旦processRows调用$ query-&gt; get(),所有后续调用第一次抛出Query error: No tables used

这有什么解决方案吗? Codeigniter 2中是否存在我不知道的分块功能?

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找的是#34; Active Record Caching&#34;。这可以从几个不同的地方进行管理。在这个答案中,它位于ProcessData()

注意: 你正在为同一个var $query分配很多东西并且经常把它传递给我,因为没有任何理由我可以看到。并且您经常连续多次用完全相同的值覆盖$query。我在您使用的大多数地方$this->db使用了$query

public function ProcessData()
{
    //Report 1
    $this->db->start_cache();
    //createSelectQuery(); not needed if you want all fields from one table
    applyReportOneWhereValues();
    $this->db->stop_cache();
    processLargeDataSet(10, 1000);

    //Report 2
    $this->db->flush_cache()
    $this->db->start_cache();
    //createSelectQuery(); not needed if you want all fields from one table
    applyReportTwoWhereValues();
    $this->db->stop_cache();
    processLargeDataSet(10, 1000);
    $this->db->flush_cache();
}

您的问题使用select("*")from("table_name")如果您真的想要从一个表中获取所有字段,则可以将其删除。如果使用get("table_name")并且没有select()调用,则会假定所有字段。 IOW,查询语句为SELECT * FROM 'table_name';

根据问题代码,您似乎不需要createSelectQuery()功能。

您的&#34;适用于&#34;函数但使用方法链重写。

public function applyReportOneWhereValues()
{
    $this->db
      ->where("table.color", "blue")
      ->where("table.size", "large")
      ->where("table.shape", "round");
}

public function applyReportTwoWhereValues()
{
    $this->db
      ->where("table.color", "red")
      ->where("table.size", "small")
      ->where("table.shape", "round");
}

我已取消processRows()并将该逻辑纳入processLargeDataSet()。请注意如何使用get() - 传递表名,限制和偏移量 - 以消除对select()from()limit()offset()调用的需求

/**
 * Process the records in chunks
 * @param int $numberOfPages The number of pages to create in the set (1 to n)
 * @param int $pageSize The number of records per page
 */
function processLargeDataSet($numberOfPages, $pageSize)
{
    if($numberOfPages < 1)
    {
        $numberOfPages = 1;
    }
    for($i = 1; $i < $numberOfPages; $i++)
    {
        $valuesToProcess = $this->db
          ->get('table', $pageSize, ($i-1) * $pageSize)
          ->result_array();
        // process the rows in $valuesToProcess
    }
}

答案 1 :(得分:1)

以下是修订后问题的新答案。

public function ProcessData()
{
    //Report 1
    $query_builder = $this->applyReportOneWhereValues($this->createSelectQuery());
    $this->db->stop_cache();
    $results1 = CSVTool::processLargeDataSet($query_builder, 10, 1000);
    $this->db->flush_cache();

    //Report 2
    $query_builder = $this->applyReportTwoWhereValues($this->createSelectQuery());
    $this->db->stop_cache();
    $results2 = CSVTool::processLargeDataSet($query_builder, 10, 1000);
    $this->db->flush_cache(); //just to be safe
}

public function createSelectQuery()
{
    $this->db->start_cache();
    return $this->db->select('ball.name, color.name, size.name, shape.name')
        ->join('color', 'ball.color_id = color.id')
        ->join('size', 'ball.size_id = size.id')
        ->join('shape', 'ball.shape_id = shape.id');
}

public function applyReportOneWhereValues($query_builder)
{
    return $query_builder
      ->where("table.color", "blue")
      ->where("table.size", "large")
      ->where("table.shape", "round");
}

public function applyReportTwoWhereValues($query_builder)
{
    return $query_builder
      ->where("table.color", "red")
      ->where("table.size", "small")
      ->where("table.shape", "round");
}

在CSVTool.php中

/**
 * Process the records in chunks
 * @param CI_DB_query_builder $qb An instance of the CI_DB_query_builder class
 * @param int $numberOfPages The number of pages to create in the set (1 to n)
 * @param int $pageSize The number of records per page
 */
public static function processLargeDataSet($qb, $numberOfPages, $pageSize)
{
    if($numberOfPages < 1)
    {
        $numberOfPages = 1;
    }
    for($i = 1; $i < $numberOfPages; $i++)
    {
        $valuesToProcess = $qb
          ->get('ball', $pageSize, $i - 1 * $pageSize)
          ->result_array();
        // process the rows in $valuesToProcess
    }
}