n1crack / datatables

Simplify your Datatables server-side processing effortlessly using our lightning-fast PHP library, streamlining your workflow seamlessly.
https://datatables.ozdemir.be/
MIT License
267 stars 90 forks source link

Support Mutiple schema with Postgresql #107

Closed ahmaddzidan closed 5 months ago

ahmaddzidan commented 5 months ago

Hey There..

I have a problem when i using this library with multiple database group with different schema, i am using postgresql database (14.7) and Codeigniter 4 (4.5.0) as PHP framework.

i have been research through google but i cant find the suitable answer. Bellow is example of my code:

public function datatables()
    {
        $dt = new Datatables(new Codeigniter4Adapter);

        $builder = $this->db->table($this->table .  'page');

        $builder->select('page.id, page.title, template.name, page.status');
        $builder->join('web_page_templates template', 'template.id=page.template_id','left');
        $builder->orderBy('page.id DESC');

        $dt->query($builder);

        return $dt->generate()->toJson();
    }

and produce error :

pg_query(): Query failed: ERROR: relation 'web_pages' does not exist LINE 2: FROM 'web_pages''page'

i think the error is explain that table not found in current default schema. when i check Codeigniter4Adapter, i think this library is not have options to use different database group, instead use default group ?

class Codeigniter4Adapter extends DBAdapter
{

    protected $db;

    public function __construct($config = null)
    {

    }

    public function connect()
    {
        $this->db = \Config\Database::connect();

        return $this;
    }

But after i try adding the db group inside connect() with group 'website', everything works and the error is gone.

Kind regards

n1crack commented 5 months ago

Can you show me a working example?

ahmaddzidan commented 5 months ago

Bellow is my working code so far:

Codeigniter4Adapter.php

class Codeigniter4Adapter extends DBAdapter
{
    protected $db;

    protected array $config;

    public function __construct($config = null)
    {
        if ($config != null) {
            $this->config = $config;
        }

        if (!isset($this->config['DBGroup']))
        {
            $this->config['DBGroup'] = 'default';
        }
    }

    public function connect()
    {

        $this->db = \Config\Database::connect($this->config['DBGroup']);

        return $this;
    }

PageModel.php

public function datatables()
    {
        $dt = new Datatables(new Codeigniter4Adapter(['DBGroup' => $this->DBGroup]));

        $builder = $this->db->table($this->table . ' page');

        $builder->select('page.id, page.title, template.name, page.status');
        $builder->join('web_page_templates template', 'template.id=page.template_id','left');
        $builder->orderBy('page.id DESC');

        $dt->query($builder);

        return $dt->generate()->toJson();
    }

I add config array variable to Codeigniter4Adapter and pass DBGroup config via construct config parameter.

n1crack commented 5 months ago

Thanks, I will update it tonight.

ahmaddzidan commented 5 months ago

okay, thanks .