mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

Issue with Model save when having multiple MongoDb connections #1755

Closed haridarshan closed 10 months ago

haridarshan commented 5 years ago

Hi,

I'm facing an issue when having multiple MongoDb Connections and updating any existing Model in collection. I'm using this package outside laravel so here my sample code.

Illuminate Database 5.8 Jessengers MongoDb 3.5

require dirname(dirname(__DIR__))."/vendor/autoload.php";

function app()
{
    return new class() {
        public function version()
        {
            return '5.8';
        }
    };
}

use Illuminate\Database\Capsule\Manager as Capsule;
use App\Models\Test;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'   => 'mongodb',
    'host'     => '192.168.1.1',
    'port'     => '27017',
    'database' => 'test',
    'username' => 'test',
    'password' => 'test@12345',
    'options'  => [
        'database' => 'test'
    ]
], 'default');

$capsule->addConnection([
    'driver'   => 'mongodb',
    'host'     => '192.168.1.1:27017, 192.168.1.2:27017',
    'port'     => '27017',
    'database' => 'test1',
    'username' => 'test1',
    'password' => 'test1@12345',
    'options'  => [
        'database' => 'test1',
       'replicaSet' => 'rs0'
    ]
], 'test1');

$capsule->getDatabaseManager()->extend('mongodb', function ($config) {
    return new Jenssegers\Mongodb\Connection($config);
});

Eloquent::setConnectionResolver($capsule->getDatabaseManager());
$capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher(new \Illuminate\Container\Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();

$getData = Test::all();

if ($getData->isNotEmpty()) {
    foreach ($getData as $data) {
        $data->text = "Hello World!";
        $data->save();

        var_dump($data);
    }
} else {
    $test = new Test;
    $test->text = "Hello";
    $test->save();
}

When I execute the script, object I get is this

Test Object
(
    [connection:protected] => 
    [collection:protected] => test
    [timestamps] => 1
    [dateFormat:protected] => Y-m-d H:i:s
    [primaryKey:protected] => _id
    [parentRelation:protected] => 
    [table:protected] => 
    [keyType:protected] => int
    [incrementing] => 1
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 
    [attributes:protected] => Array
        (
            [_id] => MongoDB\BSON\ObjectId Object
                (
                    [oid] => 5cc843d6ff4e5860f7538c82
                )

            [text] => Hello
            [updated_at] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1556630106000
                )

            [created_at] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1556628438000
                )

        )

    [original:protected] => Array
        (
            [_id] => MongoDB\BSON\ObjectId Object
                (
                    [oid] => 5cc843d6ff4e5860f7538c82
                )

            [text] => Hello
            [updated_at] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1556630106000
                )

            [created_at] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1556628438000
                )

        )

    [changes:protected] => Array
        (
            [text] => Hello
            [updated_at] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1556630106000
                )

        )

    [casts:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [dispatchesEvents:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [relations:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

)

Amazing thing to notice is $connection is not set even though in Model I've protected $connection = 'test1'; set and response result after save is 1 but document is not updated in the db.

Model Structure

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Moloquent;

class Test extends Moloquent
{
    protected $connection = 'test1';

    protected $collection = 'test';

    public $timestamps = true;
}

Also, if I change test1 to default, update works. So, please help me out

GromNaN commented 10 months ago

We are not supporting usage outside of Laravel Framework. I can't reproduce since the provided example was for Laravel 9. Please leave a comment with an up-to-date example if you still have a problem that we can solve.