KoolReport's Forum

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

How to extend class SQLHandler? #3327

Closed jernej opened this topic on on Aug 22 - 4 comments

jernej commented on Aug 22

Hi,

I need to modify how SEARCH works, so that it will search more loosely.

So in the following code (in executes method of SQLHandler class), I need to add two additional orWhereRaw calls, as such:

    if($this->search()!==null) {
        $search = $this->search();
        $query->where(function ($query) use ($search) {
            foreach($search["fields"] as $field) {
                $query->orWhereRaw('REPLACE('.$field.', " ", "") LIKE "%'.$search["text"].'%" ');
                $query->orWhereRaw($field.' LIKE "%'.str_replace(" ", "", $search["text"]).'%" ');
                $query->orWhere($field,"LIKE","%".$search['text']."%");
            }
        });
    }

So question is: how can I do that (without modifying vendor src class), or if there is maybe a better alternative to achieve the same result?

Thank you, Jernej

KoolReport commented on Aug 23

It is possible to do that without changing code in vendor.

In any Widget that you need to use your own SQLHandler, you write a method createDataHandler like this:

class MyAnyWidget
{
        protected function createDataHandler($source)
    {
        //Process the source to common type
        switch($this->sourceType($source))
        {
            case "Query":
                return SQLHandler::create()->source($source); //Change to MyOwnSQLHandler
            break;
            case "Process":
                return PipeHandler::create()->source($source);
            break;
            case "File":
                return PipeHandler::create()->source($source->buildDataPipe());
            break;
            case "Array":
                $source = Util::arrayToDataStore($source);
            case "DataStore":
                return PipeHandler::create()->source(new StoreDataSource($source));
            break;
        }
        throw new \Exception("Could not catch type of source");
        return null;
    }
}

Now you see that there is SQLHandler in above code, change it to your MyOwnSQLHandler.

You create your own MyOwnSQLHandler class that derived from SQLHandler and you overwrite the executes method to meet your need.

Hope that helps.

jernej commented on Aug 26

Thanks for the answer. But how do I do that with Dashboard listScreen? Because I need the search in that screen to work differently.

KoolReport commented on Aug 26

I see, so you use with the Admin Panel feature of Dashboard Framework. Please do this:

First, create a derived class of AdminTable with createDataHandler

class MyAdminTable extends \koolreport\dashboard\admin\widgets\AdminTable
{
    protected function createDataHandler($source)
    {
        //Process the source to common type
        switch($this->sourceType($source))
        {
            case "Query":
                return SQLHandler::create()->source($source); //Change to MyOwnSQLHandler
            break;
            case "Process":
                return PipeHandler::create()->source($source);
            break;
            case "File":
                return PipeHandler::create()->source($source->buildDataPipe());
            break;
            case "Array":
                $source = Util::arrayToDataStore($source);
            case "DataStore":
                return PipeHandler::create()->source(new StoreDataSource($source));
            break;
        }
        throw new \Exception("Could not catch type of source");
        return null;
    }    
}

Second, create your MyOwnSQLHandler as I mentioned above.

Third, assign new MyAdminPanel to ListScreen like this in your Resource class:

class MyResource extends Resource
{
    protected function onCreated()
    {
        $this->listScreen()->adminTable(MyAdminTable::create());
    }
}

Now, the MyAdminTable with new createDataHandler will replace our default AdminTable.

Let us know if it works.

jernej commented on Aug 26

Thank you, it works perfectly!

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

None