doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

Fatal error: Uncaught TypeError: MongoDB\BSON\ObjectId::__construct() #2199

Closed nationdata-it closed 4 years ago

nationdata-it commented 4 years ago

Bug Report

Q A
BC Break yes/no
Version 2.1.1

Summary

when querying the database it generates error

`file.php /* @ODM\Field(type="object_id") / private $collection_object_id;

/** @ODM\Field(type="object_id") */
private $form_cabinet_id;

/** @ODM\Field(type="object_id") */
private $employee_id;

`

$files = $dm->createQueryBuilder(File::class) ->field('collection_object_id')->equals(new ObjectId($collection_object_id)) ->field('employee_id')->equals(new ObjectId($session_user)) ->field('form_cabinet_id')->exists(false) ->getQuery() ->execute();

or $criteria = [ 'collection_object_id' => new ObjectId($collection_object_id), 'employee_id' => new ObjectId($session_user), 'form_cabinet_id' => ['$exists' => false] ]; $dm->getRepository(self::getClassName())->findBy($criteria, $sort)

Current behavior

Fatal error: Uncaught TypeError: MongoDB\BSON\ObjectId::__construct() expects parameter 1 to be string, boolean given in /var/www/project/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php on line 20

How to reproduce

Expected behavior

should return records from db

Steveb-p commented 4 years ago

@nationdata-it please provide a stacktrace.

malarzm commented 4 years ago

@nationdata-it at a glance it seems that either $collection_object_id or $session_user is a bool.

nationdata-it commented 4 years ago

yes initial i was thinking that i'm sending wrong values to confirm that i changed the call to something like this.

$collection_object_id = new ObjectId('5ecd55047ac60e4c2c1f1234'); $session_user = new ObjectId('5e6843962aca3953e42efd00'); $criteria = [ 'collection_object_id' => $collection_object_id, 'employee_id' => $session_user, 'form_cabinet_id' => ['$exists' => false] ]; $dm->getRepository(self::getClassName())->findBy($criteria, $sort)

nationdata-it commented 4 years ago

stack trace @Steveb-p

( ! ) Fatal error: Uncaught TypeError: MongoDB\BSON\ObjectId::construct() expects parameter 1 to be string, boolean given in /var/www/project/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php on line 20 ( ! ) TypeError: MongoDB\BSON\ObjectId::construct() expects parameter 1 to be string, boolean given in /var/www/project/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php on line 20 Call Stack

Time Memory Function Location

1 0.0037 414904 {main}( ) .../index.php:0 2 0.2908 4849104 src\helpers\Slim->run( ) .../index.php:54 3 0.2908 4849080 Slim\Middleware\MethodOverride->call( ) .../Slim.php:1300 4 0.2966 4880344 Slim\Middleware\Flash->call( ) .../MethodOverride.php:92 5 0.2966 4880720 src\helpers\Slim->call( ) .../Flash.php:85 6 0.3010 5046472 Slim\Route->dispatch( ) .../Slim.php:1355 7 0.3792 5645520 call_user_func_array:{/var/www/project/vendor/slim/slim/Slim/Route.php:468} ( ) .../Route.php:468 8 0.3792 5645584 Slim\Route->Slim{closure:/var/www/project/vendor/slim/slim/Slim/Route.php:168-174}( ) .../Route.php:468 9 0.6279 8222752 call_user_func_array:{/var/www/project/vendor/slim/slim/Slim/Route.php:173} ( ) .../Route.php:173 10 0.6279 8222760 src\controllers\Controller_Dashboard->index( ) .../Route.php:173 11 0.7456 8717896 Doctrine\ODM\MongoDB\Repository\DocumentRepository->findBy( ) .../controller_dashboard.php:47 12 0.7681 9349736 Doctrine\ODM\MongoDB\Persisters\DocumentPersister->loadAll( ) .../DocumentRepository.php:160 13 0.7681 9349736 Doctrine\ODM\MongoDB\Persisters\DocumentPersister->prepareQueryOrNewObj( ) .../DocumentPersister.php:504 14 0.7682 9350864 Doctrine\ODM\MongoDB\Persisters\DocumentPersister->convertToDatabaseValue( ) .../DocumentPersister.php:1015 15 0.7682 9350864 Doctrine\ODM\MongoDB\Persisters\DocumentPersister->convertToDatabaseValue( ) .../DocumentPersister.php:1045 16 0.7682 9350864 Doctrine\ODM\MongoDB\Types\ObjectIdType->convertToDatabaseValue( ) .../DocumentPersister.php:1069 17 0.7682 9350944 __construct ( )

nationdata-it commented 4 years ago

query debug:

`Array ( [0] => Array ( [type] => 1 [query] => Array ( [collection_object_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5ecd55047ac60e4c2c1f266e )

                [employee_id] => MongoDB\BSON\ObjectId Object
                    (
                        [oid] => 5e6843962aca3953e42e2d99
                    )

                [form_cabinet_id] => Array
                    (
                        [$exists] => 
                    )

            )

        [newObj] => Array
            (
            )

    )

)`

malarzm commented 4 years ago

Ah this seems to be connected with #2194 and #2193 - it seems we're too eagerly trying to convert values ignoring expressions

malarzm commented 4 years ago

@nationdata-it would you mind checking if #2205 fixes your issue?

malarzm commented 4 years ago

Closing as #2205 was merged