KoolReport's Forum

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

Use existing PDO connection #1220

Open Ruth Wester opened this topic on on Dec 16, 2019 - 1 comments

Ruth Wester commented on Dec 16, 2019

I am a newbie to KoolReport and am adding it to an existing website which already has a PDO connection call $conn to MySQL server. How do I use the existing connection instead of creating another one?

KoolReport commented on Dec 19, 2019

Sorry for our late reply. We have no datasource for existed PDO so you can create one like this:

1- Create a ExistedPdoDataSource.php with following content:

class ExistedPdoDataSource extends \koolreport\datasources\PdoDataSource
{
    protected function onInit()
    {
        $this->connection = Utility::get($this->params,"pdo");
    }
}

2- In your report, you can use the above datasource like this:

require_once "ExistedPdoDataSource.php";

class MyReport extends \koolreport\KoolReport
{
    function settings()
    {
        return array(
            "dataSources"=>array(
                "class"=>ExistedPdoDataSource::class,
                "pdo"=>$this->params["pdo"]
            )
        );
    }
    ...
}

3 - To render report, you do this

require_once "/path/to/koolreport/core/autoload.php";
require_once "MyReport.php";

$report = new MyReport(array(
    "pdo"=>$conn
));
$report->run()->render();

Hope that helps.

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
wiki
solved

None