Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How can regular expressions be used in Doctrine ODM 2.0.2 and PHP 7.3?

$
0
0

I am using symfony 4.3, PHP 7.3, doctrine/mongodb-odm-bundle 4.0.0, doctrine/mongodb-odm 2.0.2. There was a need to use regular expression in the request. It seems that the communication of Doctrine ODM with the MongoDB driver goes through the "strip" - an intermediate alcaeus/mongo-php-adapter driver that emulates working with the Mongo driver for Doctrine.

When I try to use the \MongoRegex class

$qb->field('files.qualities.path')->equals(new \MongoRegex($regexp));

I get an error

Attempted to load class "MongoRegex" from the global namespace. Did you forget a "use" statement?

Well, that might be logical. When I try to use the MongoDB\BSON\Regex class

use MongoDB\BSON\Regex;

$qb = $this->dm->createQueryBuilder(MediaFile::class);
if ($path = $request->query->get('path', null)) {
    $regexp = '/' . str_replace('/', '\/', $path) . '/';
    $qb->field('files.qualities.path')->equals(new Regex($regexp));
}
return $this->paginator->paginate($qb, $request->query->getInt('page', 1), 30);

the query returns an empty collection (the value of $regexp is "/11.35\\/live0.m3u8/"):

vagrant@homestead$ curl -X GET "http://homestead.test/api/v0/media-files?page=1&path=11.35%2Flive0.m3u8"
[]

At the same time, a direct regular expression query in MongoDB returns the expected result:

> db.MediaFile.find({"files.qualities.path": /11.35\/live0.m3u8/}).pretty()
{
    "_id" : ObjectId("5db6de4dfdce27091222c414"),
    "files" : [
        {
            "main_path" : "/hls/2/11.35/index.m3u8",
            "qualities" : [
                {
                    "path" : "/hls/2/11.35/live0.m3u8",
                    "size" : "1920x1080"
                }
            ]
        }
    ]
}

Non-regex code is guaranteed to work. A curl request without specifying the path parameter in the URL returns the results of the selection. What am I doing wrong? How to get regexp result using Query Builder?


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>