dimitriBouteille / wp-orm

WordPress ORM with Eloquent, an object-relational mapper that makes it enjoyable to interact with your database.
https://packagist.org/packages/dbout/wp-orm
MIT License
46 stars 5 forks source link

[FEATURE] Add Illuminate\Support\Facades\DB facade #64

Open rafaucau opened 2 months ago

rafaucau commented 2 months ago

I'm encountering an issue when trying to use DB::raw. The error message indicates a problem with the facade root not being set.

//...
use Illuminate\Support\Facades\DB;
//...
TestStat::upsert(
    [[
        'test_id' => $test_id,
        'count' => 1,
    ]],
    [ 'test_id' ],
    [ 'count' => DB::raw( 'count + 1' ) ]
);

PHP Fatal error: Uncaught RuntimeException: A facade root has not been set. in /var/www/vhosts/localhost/vendor/illuminate/support/Facades/Facade.php:352

How can I properly use DB::raw in WordPress context to enable raw SQL expressions?

rafaucau commented 2 months ago

Workaround:

use Dbout\WpOrm\Orm\Database;

Database::getInstance()->raw( 'count + 1' );

Or in a mu-plugin add facade to use DB:

use Dbout\WpOrm\Orm\Database;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Facade;

$container = new Container();
$container->instance( 'db', Database::getInstance() );
Facade::setFacadeApplication( $container );
dimitriBouteille commented 2 months ago

Hi @rafaucau , thank you for reporting this.

The facades are unfortunately not supported by the library since it is a few things that are mainly used by Laravel projects.

I will try to add these facades to the project but I am not sure it is possible since you have to go through the Illuminate container system. Unfortunately, this requires adding dependencies to projects:(

The only solution today is indeed what you propose in your message.