KoolReport's Forum

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

How to reference Datasource when Piping (to multiple widgets) #2963

Open Steve Feland opened this topic on on Jan 27, 2023 - 1 comments

Steve Feland commented on Jan 27, 2023

Hi team - I've been leveraging the dashboard package and template but have some questions on piping.

I would like to: . set up a datasource for each dashboard, . query the database (most of the widgets would leverage the same data queried from db), then *. pipe the data into different datastores (to be consumed by like widgets)

I want to use different datastores depending on the needs of the widget (some datastores could supply multiple widgets).

Question 1: what would be the right architecture to do that? I am using a BaseReport.php as described previously [https://www.koolreport.com/forum/topics/67](Topic 67) for my Settings and Setup. Question 2: How do I reference and use the datastore? (I couldn't find any examples in the demo of it being used)

<?php
//BaseReport.php to hold like settings

namespace demo\headcount;

use demo\AutoMaker;
class BaseReport extends Automaker
{
    protected function settings()
     {    
            return array(
                "dataSources"=>array(
		    'dsemployee'=>array(
        		'connectionString'=>'mysql:host=localhost;dbname=talentanalytics',
        		'username'=>'root',
        		'password'=>'',
        		'charset'=>'utf8'
    			),
                )
            );
    }

    public function setup()
    {
        $this->src('dsemployee')
	->query
		(DB::table("eetable")
		->select("Hire_Date","End_Date")
                ->where("COID",$coid)
        	)
        	->pipe($this->dataStore("mydata"))
 		->requestDataSending(); //populate data to the datastore
    }
}

<?php
// HeadcountBoard.php uses BaseReport

namespace demo\headcount;

use demo\BaseReport;
<?php
//TotalEmployee.php contains SimpleCard widget

namespace demo\headcount;
use koolreport\dashboard\widgets\SimpleCard;

class TotalEmployees extends SimpleCard
{

    protected function process()
    { //nothing to process
    }

    protected function value()
    {
        //Get value from the date range picker
        $range = $this->sibling("HeadcountDateRange")->value();

/* THIS Errors out:  return BaseReport::create(array("dataSource"=>$this->dataStore('mydata')))
	Message: Call undefined dataStore() method
	QUESTION: How do I use reference PIPED datasource?  */

//This one works when query directly
        return BaseReport::table("eetable")
		->count('Hire_Date')
                ->whereBetween("Hire_Date",$range)
		->run()
		->getScalar();
    }

    protected function onCreated()
    {
        $this
        ->type("primary")
        ->text("Employees")
        ->icon("fas fa-users");
    }

}
Sebastian Morales commented on Jan 30, 2023

A report or datastore is different from a Dashboard's datasource. You can not mix them together. Certainly you can run your BaseReport to get its datastore objects to use inside a Dashboard's widget but you can not use query builder on such datastores.

It's my advice that you copy the datasource setting of BaseReport to your Dashboard's datasource and copy query building in BaseReport's setup to Dashboard's widgets. It's not always worth it to reuse code from a different type of object.

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

Dashboard