leroy-merlin-br / mongolid

Easy, powerful and ultrafast ODM for PHP and MongoDB
MIT License
109 stars 27 forks source link

BUG : Schema name is always mongolid. #237

Open RodrigoBLISS opened 3 weeks ago

RodrigoBLISS commented 3 weeks ago

Hello everyone, There is a bug where the defaultServerName is not being updated on the right time. This makes the schema always be "mongolid".

Look at this example (following documentation):

$manager = new Manager(); $manager->setConnection(new Connection('mongodb://localhost:27017/AwesomeSchema'));

Connection.php has a method called : findDefaultDatabase() That extracts the schema name from the connection string with a regex.

But its being called too late. I should be called inside __construct method to update the database name.

What happens is Manager SetConnection pulls the default database name from the connection object, but since the findDefaultDatabase() was not called, the schema name was not updates and stays 'mongolid'.

This is Connection.php construct() : public function construct( string $server = 'mongodb://localhost:27017', array $options = [], array $driverOptions = [] ) { $this->server = $server; $this->options = $options; $this->driverOptions = $driverOptions; }

We must include : 
$this->findDefaultDatabase($this->server);

Adding this will fix the issue. Updating the database name during instantiation,
so Manager can populate itself with the correct values.

Currently its only being updated with the 'getClient' method.. and its too late.

Best regards.
RodrigoBLISS commented 3 weeks ago

Hello, I've created a PR to fix this issue.