Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Make it compatible with multiple database connections in different servers? #8

Closed
rust17 opened this issue Feb 1, 2021 · 2 comments
Labels
good first issue Good for newcomers

Comments

@rust17
Copy link

rust17 commented Feb 1, 2021

Hi, It's not a bug but a feature want to be added;
Is it possible to make it available for crossing multiple database connection?
For example

// App\Models\Company
class Company extends Model
{
    .
    .
    .
    public function supplier()
    {
        return $this->hasOne(Supplier::class);
    }
    .
    .
    .
}

// App\Models\Supplier
class Supplier extends Model
{
    .
    .
    .
    public function __construct($attributes = [])
    {
        parent::__construct($attributes);

        $connection = config('database.supply');

        $table = $this->getTable();

        $this->setConnection($connection);

         $this->setTable(config('database.connections.'.$connection.'.database').'.'.$table);
    }
    .
    .
    .
}

When I need to get the Company models that contains supplier, using laravel wherehas

Company::whereHas('supplier')->get();

It's not support. For detail, see #33402. The main reason is that laravel merge two tables into one sql. Like:

select count(*) as aggregate from `companies` where exists (select * from `supply`.`suppliers` where `companies`.`id` = `supply`.`suppliers`.`company_id`))

It will ignore database1 connection.

By using hasByNonDependentSubquery:

Company::hasByNonDependentSubquery('supplier')->get();

Sql :

select count(*) as aggregate from `companies` where `companies`.`id` in (select `supply`.`suppliers`.`company_id` from `supply`.`suppliers`))

Also just one sql. It's it possible to make it split to two sql? Thanks a lot.

@mpyw
Copy link
Owner

mpyw commented Feb 1, 2021

@rust17 I've checked the following package.

While it looks useful, the implementation of the library is quite complex. I'm worrying about we get into spaghetti codes. Since the macros provided by my library focuses only on the generation of the query and leave its execution to the user, it's so hard to separate it into two queries.

Personally, I think it would be better to explicitly separate queries when crossing databases. If you are not willing to write such statements in controllers or models, try the CleanArchitecture-like pattern that introduces UseCase classes.

@mpyw mpyw closed this as completed Feb 1, 2021
@mpyw mpyw pinned this issue Feb 1, 2021
@rust17
Copy link
Author

rust17 commented Feb 6, 2021

This package I have already tried. It add the database and table prefix in the sql but still one sql. When cross different server database, as I mentioned one sql don't work .

It will ignore database1 connection

I think may be fork it to make a new package. Thanks for reply. By the way, your article is great. I may try to use the use case in my project.

@mpyw mpyw added the good first issue Good for newcomers label Feb 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants