KoolReport's Forum

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

Access specific column on function setup() #2511

Closed Ron opened this topic on on Dec 29, 2021 - 4 comments

Ron commented on Dec 29, 2021

I have the following code

class Profession_schedule extends \koolreport\KoolReport
{
    use \koolreport\amazing\Theme;

    var $profession_name = "";

    function setup()
    {
        $this->src("db")
        ->query('CALL getProfessionSchedule(:year, :days, :profession_id)')
        ->params(array(
            ':year' => $this->params['year'],
            ':days' => $this->params['days'],
            ':profession_id' => $this->params['profession_id'],
        ))
        ->saveTo($node1);
        $node1->pipe(new Cube(array(
            'row' => 'hour_num',
            'column' => 'day',
            'max' => "teacher_name",
        )))
        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $k => $v)
                    if ($v == null) $row[$k] = "";
                        return $row;
            }
        )))
        ->pipe(new koolreport\processes\ColumnsSort(array(
            "{name}" => function($colName1, $colName2)
            {
                return $colName1>$colName2;
            }
        )))
        ->pipe($this->dataStore("st"));
    }
}

one of the columns in the datasource is called "profession_name". I want to set the class private variable $this->profession_name with the value in the column "profession_name" without using a Map Process

Sebastian Morales commented on Dec 30, 2021

You can use method ->requestDataSending() after piping to a datastore to populate its data right in setup() like this:

function setup()
{
    $this->src(...)
    ->pipe(...)
    ->pipe($this->dataStore("myds"))
    ->requestDataSending();

    $this->profession_name = $this->dataStore("myds")->only("profession_name")->data();
    print_r($this->profession_name);
}
Ron commented on Dec 30, 2021

When I var_dump() the data_source before the Cube I still see the "profession_name" column. but after it like you suggested using the ->requestDataSending(); I don't have this column in the data_source anymore! I need an option to access it before the Cube!

Sebastian Morales commented on Dec 30, 2021

We should probably separate the pipe from the beginning:

        $this->src("db")
        ->query('CALL getProfessionSchedule(:year, :days, :profession_id)')
        ->params(array(
            ':year' => $this->params['year'],
            ':days' => $this->params['days'],
            ':profession_id' => $this->params['profession_id'],
        ))
        ->pipe($this->dataStore("myds"))
        ->requestDataSending();

        $this->profession_name = $this->dataStore("myds")->only("profession_name")->data();
        print_r($this->profession_name);

        $this->src("db")
        ->query('CALL getProfessionSchedule(:year, :days, :profession_id)')
        ->params(array(
            ':year' => $this->params['year'],
            ':days' => $this->params['days'],
            ':profession_id' => $this->params['profession_id'],
        ))
        ->saveTo($node1);
        $node1->pipe(new Cube(array(
            'row' => 'hour_num',
            'column' => 'day',
            'max' => "teacher_name",
        ))) 
        ...

Let us know how this works. Rgds,

Ron commented on Dec 30, 2021

ok. if that is the case then we it is better to transfer the profession_name as an external parameter rather to execute the query to the database twice. anyhow thank you for the explanation.

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
None yet

None