Laravel mongodb ->getPdo() return null - php

I'm using DB::connection('mongodb')->getPdo() to check if the database is connected (if not phpunit will mark some test as skipped) and it's turning out that ->getPdo() always return null. The connection is working proven by that I can use php artisan migrate to create new collection and I can insert data into the collection.
Environment :
OS : windows 10 running MAMP 3.2.0
laravel : 5.2.37
jenssegers/mongodb : 3.0.2
Edit 1: Add returned object from using DB::connection('mongodb')->getMongoDB()->connected
MongoDB\Collection {#647
+collectionName: "connected",
+databaseName: "destinycore",
+manager: MongoDB\Driver\Manager {#640},
+readConcern: MongoDB\Driver\ReadConcern {#642},
+readPreference: MongoDB\Driver\ReadPreference {#643},
+typeMap: [
"array" => "MongoDB\Model\BSONArray",
"document" => "MongoDB\Model\BSONDocument",
"root" => "MongoDB\Model\BSONDocument",
],
+writeConcern: MongoDB\Driver\WriteConcern {#644},
}

While confusing, it's actually correct. This connection class extends from the base class provided by Laravel. It doesn't return a PDO instance because this is a MongoDB connection. It doesn't actually use PDO, which is only used for SQL-related databases. If you're looking for the "raw" connection for this package, you should use the getMongoDB method, which returns a MongoDB\Client instance.

Related

PHP-DI / Definition for resolving every time a new object

i am trying to update phpdi from 5.4.6 to 6.4 and i don't know how to write a definition for objects which should be created everytime it is injected.
In 5.4.6 i simly wrote
return [
'setasign\\Fpdi\\Fpdi' => DI\object()->scope(Scope::PROTOTYPE()),
]
But in 6.4 DI\object()->scope(Scope::PROTOTYPE()) does not exists anymore.
I read the documentation https://php-di.org/doc/scopes.html and understand that I can now use $container->make() instead of $container->get(), but then i have to rewrite much code in my repository.
Is there an option to resolve the problem in my definitionfile directly?
Thanks
With this way, i get the same instance every time i call $container->get()
setasign\\Fpdi\\Fpdi' => DI\factory(
function () {
return new setasign\Fpdi\Fpdi();
}
)

How to create MongoDB views in PHP using MongoDB\Client() library?

I'm using following:
PHP 7.2
MongoDB 3.4
Pecl 1.5.2
I'm working on a Laravel project. It uses MongoDB as database. I have few collections on which I have to create Mongo Views using Laravel migration. I was wondering whether its possible to create Mongodb Views using PHP. Currently I have a work around. I have created a JavaScript file which has MongoDB db.createView() query in it. It also takes view name and collection name as parameters. Following is my work around. $db has database name, $view has view name, $collection has collection name and $script has the path to the JavaScript file. This code I'm writing in migration class's up() method.
$cmd = "mongo $db --eval \"var view='$view', collection='$collection'\" $script";
exec($cmd);
In my Javascript file, I have code something like following
db.createView(view, collection, <aggregate query>);
So as everyone can see, I'm running terminal command from PHP to make views. So is there any PHP function in mongo library to make mongo views?
If you're using mongo with Laravel, I'm going to assume you're using jenssegers/mongodb to use it with Eloquent.
So, let's assume you have your mongo database set up as your 'mongodb' database connection. You need the MongoDB\Database for your database. You can get this with:
$mongo = app('db')->connection('mongodb')->getMongoDB();
Of course, if you're not using jenssegers/mongodb, you can still do the same thing with mongodb/mongodb as well.
$mongo = (new MongoDB\Client)->selectDatabase($db);
This has a method called command (see https://docs.mongodb.com/php-library/current/reference/method/MongoDBDatabase-command/), which corresponds to the db.runCommand method from the mongo cli. db.createView calls that method (see https://docs.mongodb.com/manual/reference/method/db.createView/#db.createView)
So, you can use $mongo->command to create the view like this:
$mongo->command([
'create' => $view,
'viewOn' => $collection,
'pipeline' => $aggregateQuery,
'collation' => ['locale' => 'en'],
]);
You can use this library mongoPhpLibrary
This will make your work easy

Laravel functional testing ajax control

In laravel 5.4, I can see that there are methods such as:
$browser->pause(1000);
$browser->waitFor('.selector');
$browser->waitForLink('Create');
But I don't see these in Laravel 5.3.
We have two chained select boxes where second select box values are loaded through ajax based on selection from first select box. The problem is that when we run test, laravel doesn't wait for second selectbox/ajax to load which makes the test fail because it could not select any value from second select box.
$this->visit('/department');
$this->select('1', 'country_id');
$this->select('1', 'region_id'); // problem here
// rest of code
I also tried using sleep() but it didn't work.
Any idea of how to functional test such scenario in 5.3 please ? Thanks
By default, laravel 5.3 doesn't support this function. As they have introduced Ajax Testing in laravel 5.4 using Dusk.
Check this post : https://laravel-news.com/laravel-dusk-is-coming
However, We are in luck.
Looking at the composer.json of dusk. You can use it in laravel 5.3 as its dependency is "illuminate/support" : "~5.3" which is satisfied by the Laravel 5.3.
All you need to do is : composer require laravel/dusk
Check the composer.json here : https://github.com/laravel/dusk/blob/master/composer.json
Edit:
There was an issue with the dependency. I created a new package which resolved the dependency issue. I have run all the test cases. It didn't give me any error.
You can use this package using following command : composer require pankitgami/dusk
Check here : https://packagist.org/packages/pankitgami/dusk
seeJsonEquals used for Verify Exact JSON Match
$this->post('/user', ['name' => 'Sally'])
->seeJsonEquals([
'created' => true,
]);

Call stored procedure/function with PHP in Mongo DB sharded cluster

I am using MongoDB 2.6 with two shard clusters config.
I want to call a function dataStats() that I create and store in MongoDB. This is my PHP script:
$client = new Mongo();
$db = $client->mydata;
$db->system->js->save(array("_id"=>"dataStats",
"value"=>new MongoCode("function() { ... }")));
$db->execute("dataStats()");
This code gives me this error:
'err' => 'Error: can\'t use sharded collection from db.eval',
'code' => 16722
The reason is $db->execute method is using Mongo db.eval command which is not supported with sharded collections. Is there a workaround for this issue? How can we call a stored procedure in sharded MongoDB from PHP?
There's no workaround. db.eval doesn't work with sharded collections. You should avoid using it if at all possible, anyway.

How can I confirm PHP MongoDB connection is properly recognizing replica sets?

Using the example from the manual:
$mongo = new Mongo("mongodb://sf2.example.com,ny1.example.com", array("replicaSet" => "myReplSet"));
When I check $mongo, it says it is indeed connected. I thought I could then call $mongo->isMaster() to get replica set details, but that doesn't work. Is that not a proper way of doing this?
isMaster isn't a PHP function (see http://www.php.net/manual/en/class.mongo.php for a list of functions available in the Mongo class). You can do:
$result = $mongo->myDb->command(array("isMaster" => 1));
This runs the isMaster command on the myDb database (it doesn't matter what db you run it on).

Categories