簡體   English   中英

如何調用Codeigniters 2 Active Record從同一查詢中獲取2組結果?

[英]How do I call Codeigniters 2 Active record to get 2 sets of results from the same query?

我本質上是在為文件導出創建分塊功能,並且我已經在其中建立了select-> from->。 我希望每次可以使用不同的限制/偏移值兩次調用“ get”。

這是基本概念的演練。

// 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-> get(),所有后續調用都將引發Query error: No tables used

有什么解決辦法嗎? 我沒有意識到Codeigniter 2中的分塊功能嗎?

我認為您正在尋找的是“活動記錄緩存”。 可以從幾個不同的地方進行管理。 在這個答案中,它在ProcessData()

注意:您正在為相同的var $query分配很多東西,並且在沒有充分理由的情況下將其大量傳遞。 而且,您經常連續多次用完全相同的值覆蓋$query 我在您使用$query大多數地方都使用過$this->db

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()函數。

您的“應用在哪里”功能起作用,但使用方法鏈重寫。

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
    }
}

這是對修訂后的問題的新答案。

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM