KoolReport's Forum

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

Codeigniter 4 database in koolreport #1529

Closed pargibay opened this topic on on Jul 13, 2020 - 4 comments

pargibay commented on Jul 13, 2020

Hi, I installed codeigniter 4 and koolreport with composer but codeigniter package cannot find codeigniter database Regards

KoolReport commented on Jul 15, 2020

Please locate the "config/databases.php" and look for the connection name. The name of connection inside KoolReport is the same with that.

pargibay commented on Jul 15, 2020

Hi, The connection name is "default" but the output message is "There is no source available, please add at least one in the settings()"

This is my code in report:

<?php 
require APPPATH."../vendor/autoload.php";

use \koolreport\processes\Group;
use \koolreport\processes\Join;
use \koolreport\processes\Limit;
use \koolreport\processes\Sort;

//Define the class
class Lot extends \koolreport\KoolReport
{
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\bootstrap4\Theme;

    function defaultParamValues()
    {
        return array(
            "lot"=>0
        );
    }

    function bindParamsToInputs()
    {
        return array(
            "lot"
        );
    }

    public function setup()
    {
        $this->src('default') //<== 
        ->query("SELECT name lot FROM  lots ORDER BY catchdate DESC") 
        ->pipe($this->dataStore('lots'));

If I put the database settings this report works but I have to put it in every report

Regards

KoolReport commented on Jul 16, 2020

Please look at this [topic #67] (https://www.koolreport.com/forum/topics/67), it will give you an idea to solve this.

pargibay commented on Jul 17, 2020

Hi Koolreport,

I modified codeigniter Friendship.php to capture default database in codeigniter 4.

Maybe in the next update you could add

Regards

<?php

namespace koolreport\codeigniter;

use \koolreport\core\Utility;

trait Friendship
{
    public function __constructFriendship()
    {

        //assets folder
        $assets = Utility::get($this->reportSettings, "assets");
        if ($assets == null) {
            $document_root = Utility::getDocumentRoot();
            $script_folder = str_replace("\\", "/", realpath(dirname($_SERVER["SCRIPT_FILENAME"])));
            $asset_path = $script_folder . "/assets";
            $asset_url = Utility::strReplaceFirst($document_root, "", $script_folder) . "/assets";
            if (!is_dir($asset_path . "/koolreport_assets")) {
                if (!is_dir($asset_path)) {
                    mkdir($asset_path, 0755);
                }
                mkdir($asset_path . "/koolreport_assets", 0755);
            }

            $assets = array(
                "url" => $asset_url . "/koolreport_assets",
                "path" => $asset_path . "/koolreport_assets",
            );
            $this->reportSettings["assets"] = $assets;
        }
////////////////////////////////////start\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
      //If codeigniter 4 then load default database <====
        if(file_exists(APPPATH . 'Config/App.php')){
                $db = \Config\Database::connect();
                $dataSources = array(
                   "default"=>array(
                       "connectionString"=>"mysql:host=$db->hostname;dbname=$db->database",
                       "username"=>$db->username,
                       "password"=>$db->password,
                        "charset"=>$db->charset
                      ),
                  );
                $this->reportSettings["dataSources"] = $dataSources; 
        }
        return;
////////////////////////////////////end\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        if (!file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')
            && !file_exists($file_path = APPPATH . 'config/database.php')) {
            return;
        }

        include $file_path;
        $dbSources = array();
        foreach ($db as $name => $dbconfig) {
            $dbSources[$name] = array(
                "class" => CIDataSource::class,
                "name" => $name,
            );
        }
        $dataSources = Utility::get($this->reportSettings, "dataSources", array());
        $this->reportSettings["dataSources"] = array_merge($dbSources, $dataSources);

    }
}

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
suggestion

CodeIgniter