Laravel-mongodb: Laravel MongoDB is not fully compatible with Laravel 5.4

Created on 28 Jan 2017  路  7Comments  路  Source: jenssegers/laravel-mongodb

PHP error: Call to a member function compileSelect() on null
and more errors

Most helpful comment

For a quicker/cleaner fix, explicitly set the connection name on your related non-mongodb model (MySQL)

class User extends Eloquent {
    protected $connection = 'mysql';
}

In more detail:

The issue seems to reside in the newRelatedInstance method in Eloquent

The problem starts here:
Jenssegers\Mongodb\Eloquent\HybridRelations@belongsTo, line 141:

139        // Check if it is a relation with an original model.
140        if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
141            return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
142        }

Then it reaches
Illuminate\Database\Eloquent\Concerns\HasRelationships@belongsTo

106        $instance = $this->newRelatedInstance($related);

And then in the same file, at line 489:

485    protected function newRelatedInstance($class)
486    {
487        return tap(new $class, function ($instance) {
488            if (! $instance->getConnectionName()) {
489                $instance->setConnection($this->connection);
490            }
491        });
492    }

But since $instance->getConnectionName() on your mysql related model has no connection set (works on the default connection so $connection is set to null), the newly created mysql model will have the connection improperly set to mongodb, which leads to issues.

All 7 comments

Same issue here !

Is there any work around?

I experience this issue when I try and access a belongsTo relation from the MongoDB Model (Invoice) which references a MySQL Model (Customer). The relation works the other way around i.e. MySQL to MongoDB fine:

App\Customer::find(1000)->invoices;
    => Illuminate\Database\Eloquent\Collection ...

App\Invoice::find('58b4beb78295c4b5f1590292')->customer;
    PHP error:  Call to a member function compileSelect() on null in .../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php on line 1650

Laravel 5.4 with "jenssegers/mongodb": "^3.2"

The issue seems to appear when you are using HybridRelations Trait :

    public function customer()
    {
        return new \Jenssegers\Mongodb\Relations\BelongsTo(Customer::query(), $this, 'customer_id', 'id', 'cutomer');
    }

work but,

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

don't work.

I think Trait usage could be the problem since Laravel Model use Concerns\HasRelationships Trait.

public function customer()
{
return new \Jenssegers\Mongodb\Relations\BelongsTo(Customer::query(), $this, 'customer_id', 'id', 'cutomer');
}

Thanks for this fix, can confirm it works nicely.

@dannysuryadi @Maxeee09 yep, it worked ok!

For a quicker/cleaner fix, explicitly set the connection name on your related non-mongodb model (MySQL)

class User extends Eloquent {
    protected $connection = 'mysql';
}

In more detail:

The issue seems to reside in the newRelatedInstance method in Eloquent

The problem starts here:
Jenssegers\Mongodb\Eloquent\HybridRelations@belongsTo, line 141:

139        // Check if it is a relation with an original model.
140        if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
141            return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
142        }

Then it reaches
Illuminate\Database\Eloquent\Concerns\HasRelationships@belongsTo

106        $instance = $this->newRelatedInstance($related);

And then in the same file, at line 489:

485    protected function newRelatedInstance($class)
486    {
487        return tap(new $class, function ($instance) {
488            if (! $instance->getConnectionName()) {
489                $instance->setConnection($this->connection);
490            }
491        });
492    }

But since $instance->getConnectionName() on your mysql related model has no connection set (works on the default connection so $connection is set to null), the newly created mysql model will have the connection improperly set to mongodb, which leads to issues.

Was this page helpful?
0 / 5 - 0 ratings