KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

RequestDataSending on temp table not working #1850

Open MarkoS opened this topic on on Jan 18, 2021 - 5 comments

MarkoS commented on Jan 18, 2021

Hi guys,

I am using TEMPORARY TABLE called Grupa with MEMORY engine to store temp data from another query. To access data stored in TEMP TABLE I am running this in Setup() function:

->requestDataSending();

I have query like below:

$this->src('automaker')
            ->query("SELECT SUM(Kolicina) UkupnaKolicina FROM cm_prodaja INNER JOIN 
            Grupa ON cm_prodaja.Grupa = Grupa.Grupa WHERE 
            DatumOtprem BETWEEN '2019-01-02' and '2019-01-11' AND cm_prodaja.Grupa like 'D%'
            ")
            ->pipe($this->dataStore("q_kolicina"))
            ->requestDataSending();

I would like to access UkupnaKolicina as final int value using line below:

    
$q_kolicina = $this->dataStore("q_kolicina")->data()[0]["UkupnaKolicina"];

The problem is that I am getting error saying that table Grupa does not exist. And that happens only when I am using ->requestDataSending(); to capture data. No matter where I place it, it just breaks things. I do not know what is going on, but requestDataSending works on all normal DB tables just fine where there is no TEMPORARY table used.

Can someone tell me how to access the value from this query?

Thx

David Winterburn commented on Jan 19, 2021

Hi Marko,

When and where did you create your temporary table?

MarkoS commented on Jan 19, 2021

Hi David,

Here is code I am using, you will get the point,

function setup()
    {       
            
            
            // Init temp table
            $this->src('automaker')->query("DROP TABLE IF EXISTS Grupa;");  
            $this->src('automaker')->query("CREATE TEMPORARY TABLE Grupa (Grupa varchar(7)) ENGINE=MEMORY;");
            
            
            // Insert data into temp table            
            $this->src('automaker')->query("INSERT INTO Grupa SELECT DISTINCT Grupa 
                FROM cm_prodaja WHERE 
                Dobavljac = 1675 .........;
            ");
           
            // push some data to the view to confirm it worked
            $this->src('automaker')->query("SELECT * FROM Grupa;")->pipe($this->dataStore("Grupa"));
            
            
            // params for query A definition goes here
            $q_kolicina = $this->src('automaker')
            ->query("SELECT SUM(Kolicina) UkupnaKolicina FROM .....
            ")
            ->params($kolicina_params);
            $q_kolicina->pipe($this->dataStore("q_kolicina"));
            
            // get the value
            $q_kolicina_final = $this->dataStore("q_kolicina")[0]["UkupnaKolicina"]; 
          
            // params for query B definition goes here
            $q_iznos = $this->src('automaker')
            ->query("SELECT SUM(Iznos) UkupniIznos FROM ......
            ")
            ->params($iznos_params);
            $q_iznos->pipe($this->dataStore("q_iznos"));
            
            // get the value
            $q_iznos_final = $this->dataStore("q_kolicina")[0]["UkupniIznos"];
       
            // the rest of the code goes here
            
            
        
    }

As you can see, I init temp table on the top. Than I can push data into and get data from it, which works fine and

$this->dataStore("q_kolicina");

works in the View like charm. Problem is that I can't access it's data using

$q_kolicina_final = $this->dataStore("q_kolicina")[0]["UkupnaKolicina"];

I need them as rest of the code and last query (not in example above) is using those values.

David Winterburn commented on Jan 19, 2021

Queries in the report's setup function don't run right away unless you call requestDataSending() at the end of pipes. So for the temporary table to exist you would have to call requestDataSending() at the end of these command as well, before requestDataSending() on the select query:

            $this->src('automaker')->query("DROP TABLE IF EXISTS Grupa;");  
            $this->src('automaker')->query("CREATE TEMPORARY TABLE Grupa (Grupa varchar(7)) ENGINE=MEMORY;");
MarkoS commented on Jan 19, 2021

Ok I see,

I have added requestDataSending to all and it stopped complaining about missing table. But I am not getting any data here:

$q_kolicina_final = $this->dataStore("q_kolicina")->data()[0]["UkupnaKolicina"];

With query using JOIN on that temp Grupa table:

$this->src('automaker')
            ->query("SELECT SUM(Kolicina) AS UkupnaKolicina FROM cm_prodaja INNER JOIN Grupa ON cm_prodaja.Grupa = Grupa.Grupa WHERE DatumOtprem BETWEEN :start and :end
            ".(($this->params["nadgrupa"]!='sve')?" AND cm_prodaja.Grupa LIKE :nadgrupa":"")."
            ")
            ->params($kolicina_params)
            ->pipe($this->dataStore("q_kolicina"))
            ->requestDataSending();
            
            $q_kolicina_final = $this->dataStore("q_kolicina")->data()[0]["UkupnaKolicina"];

I've placed ->requestDataSending(); after all queries

NOTE: I can use code below to print output in View, pls check comments in it

    echo "Print Grupa data: ";
    print_r($this->dataStore("Grupa")[0]["Grupa"]); // works fine, prints out data
    
     echo "<br>Količina: ";
     print_r($this->dataStore("q_kolicina")[0]['UkupnaKolicina']); // does not print anything any more
     print_r($this->dataStore("q_kolicina"));  // prints all query objects data
    
     echo "<br>Iznos: ";
     print_r($this->dataStore("q_iznos")[0]['UkupniIznos']); // does not print anything any more
     print_r($this->dataStore("q_iznos")); // prints all query  objects data

As you can see, getting data has been changed in View as well since I added requestDataSending after each query pipe's

MarkoS commented on Jan 19, 2021

Update on this:

I've solved it, besides requestDataSending there was wrong input for data (params data) and there was no results used for calculations. That's why server was throwing error. It's solved with simple if else :)

Thanks David, it's good to know for future.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

None