KoolReport's Forum

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

Setting a variable from a datasource #884

Closed Rooze McKelvey opened this topic on on May 23, 2019 - 3 comments

Rooze McKelvey commented on May 23, 2019

Can you help me with a workflow?

I'm pulling a project based on a name search from a form (the names are unique in the database). However, I need to grab the project ID from that datastore, set it to a variable, and use it in several other queries. For example:

  • First Query finds the project and returns the name and id:
 $this->src('teamKRS')
         ->query(MySQL::type(
           DB::table("projects")
           ->where([
             ['projects.name',$storeName],
             ['projects.in_trash',NULL]
           ])
     ->Select('projects.name')->alias('Project')
        ->AddSelect('projects.id')->alias('ID')
         ->take(1)
        ))
        ->pipe($this->dataStore("client_project_header"));  
  • The next query requires the project id to work correctly, however:
$this->src('teamKRS')
->query(MySQL::type(
  DB::table("extra_fields_list")->where([
    ['extra_fields_list.bind_id', $projID],
    ['extra_fields_list.extra_fields_id', '12'],
])
->Select('extra_fields_list.value')->alias('Address')
))
->pipe($this->dataStore("client_address"));

All of these are combined into a single report.

How can I use projects.id from dataStore("client_project_header") to set $projID before running the next query?

Thanks,

Rooze

KoolReport commented on Jun 4, 2019

Sorry for my late reply, you do:

$this->src('teamKRS')
         ->query(MySQL::type(
           DB::table("projects")
           ->where([
             ['projects.name',$storeName],
             ['projects.in_trash',NULL]
           ])
     ->Select('projects.name')->alias('Project')
        ->AddSelect('projects.id')->alias('ID')
         ->take(1)
        ))
        ->pipe($this->dataStore("client_project_header"))
        ->requestDataSending();

The method requestDataeSending() force this data pipe line to send data. And then you can use the $this->dataStore("client_project_header") in your next pipe line.

Rooze McKelvey commented on Jun 24, 2019

I need a bit more help with this. How do I use this method to set the variable so I can use it in later pipelines (which query other tables)?

So, how do I get from the first pipeline, grab the ID and use it in the next pipeline?

  //Creates datastore project_header; used for displaying project information in a single table row at the top of the report
         $this->src('teamKRS')
         ->query(MySQL::type(
           DB::table("projects")
           ->where([
             ['projects.name',$storeName],
             ['projects.in_trash',NULL]
           ])
     ->Select('projects.name')->alias('Project')
        ->AddSelect('projects.id')->alias('ID')
         ->take(1)
        ))
        ->pipe($this->dataStore("client_project_header"));
        ->requestDataSending();

Now how do I set the $projID variable so I can use it in the next query?

        //Get Extra Fields One Value at a time
        $this->src('teamKRS')
        ->query(MySQL::type(
          DB::table("extra_fields_list")->where([
            ['extra_fields_list.bind_id', $projID],
            ['extra_fields_list.extra_fields_id', '12'],
        ])
        ->Select('extra_fields_list.value')->alias('Address')
        ))
        ->pipe($this->dataStore("client_address"));

Thanks.

Rooze McKelvey commented on Jun 24, 2019

I figured it out. Thanks!

$projID = $this->dataStore('client_project_header')->pluck('ID');

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
solved

QueryBuilder