doctrine / mongodb

PHP MongoDB Abstraction Layer
https://www.doctrine-project.org/projects/mongodb.html
MIT License
442 stars 190 forks source link

Undefined index: _id after upgrading to 1.6.4 from 1.6.3 #335

Closed borodiliz closed 5 years ago

borodiliz commented 5 years ago

After upgrading to 1.6.4 from 1.6.3 I'm getting the following exception: ErrorException: Notice: Undefined index: _id.

       $builder
            ->hydrate(AlgoRegister::class)
            ->match()
            ->field('patientCenterId')->equals($patientCenterId)
            ->field('doctorCenterId')->exists(true)
            ->sort('created', 'desc')
            ->group()
            ->field('id')
            ->expression('$projectId')
            ->field('algoRegister')
            ->first('$$ROOT')
            ->replaceRoot()
            ->field('newRoot')
            ->expression('$algoRegister')
            ->sort('created', 'asc')
            ->execute()
            ->toArray();

Basically, I'm first filtering and then grouping objects by field projectId and taking the last one by creation date. Downgrading to 1.6.3 solves the problem.

Thanks in advance for your time!

Full Stack Trace:


ErrorException:
Notice: Undefined index: _id

  at vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php:2687
  at Doctrine\ODM\MongoDB\UnitOfWork->getOrCreateDocument('App\\Document\\AlgoRegister', array('newRoot' => array('_id' => object(MongoId), 'patientCenterId' => 34319, 'doctorCenterId' => 192, 'workspaceId' => 42, 'algoTemplate' => array('$ref' => 'algoTemplate', '$id' => object(MongoId)), 'algoCount' => 14, 'projectId' => 1, 'stepExecutions' => array(array('stepCount' => 0, 'stepId' => 0, 'answers' => array(array('id' => 1, 'value' => 1, 'clientDT' => object(MongoDate), 'clientTZO' => -120, 'created' => object(MongoDate), 'updated' => object(MongoDate))), 'created' => object(MongoDate), 'updated' => object(MongoDate))), 'created' => object(MongoDate), 'updated' => object(MongoDate))))
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/CommandCursor.php:140)
  at Doctrine\ODM\MongoDB\CommandCursor->hydrateDocument(array('newRoot' => array('_id' => object(MongoId), 'patientCenterId' => 34319, 'doctorCenterId' => 192, 'workspaceId' => 42, 'algoTemplate' => array('$ref' => 'algoTemplate', '$id' => object(MongoId)), 'algoCount' => 14, 'projectId' => 1, 'stepExecutions' => array(array('stepCount' => 0, 'stepId' => 0, 'answers' => array(array('id' => 1, 'value' => 1, 'clientDT' => object(MongoDate), 'clientTZO' => -120, 'created' => object(MongoDate), 'updated' => object(MongoDate))), 'created' => object(MongoDate), 'updated' => object(MongoDate))), 'created' => object(MongoDate), 'updated' => object(MongoDate))))
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/CommandCursor.php:98)
  at Doctrine\ODM\MongoDB\CommandCursor->current()
  at iterator_to_array(object(CommandCursor))
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/CommandCursor.php:217)
  at Doctrine\ODM\MongoDB\CommandCursor->toArray()
     (src/Repository/AlgoRegisterRepository.php:538)
  at App\Repository\AlgoRegisterRepository->findLastOfEachProjectForPatientCenterId(34319)
     (src/Handler/AlgoRegisterDoctorHandler.php:168)
  at App\Handler\AlgoRegisterDoctorHandler->findLastOfEachProjectForPatientCenterId(34319)
     (src/Handler/AlgoRegisterDoctorHandler.php:237)
  at App\Handler\AlgoRegisterDoctorHandler->createAlgoRegister(object(Project), object(DoctorCenter), object(PatientCenter))
     (src/Handler/AlgoRegisterDoctorHandler.php:206)
  at App\Handler\AlgoRegisterDoctorHandler->getAlgoRegisterToDay(object(Project), object(DoctorCenter), object(PatientCenter))
     (src/Controller/DoctorWellnessController.php:873)
  at App\Controller\DoctorWellnessController->patientAction(object(PatientCenter), object(Project))
     (vendor/symfony/http-kernel/HttpKernel.php:151)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:198)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)
alcaeus commented 5 years ago

1.6.4 fixed wrong syntax for the $replaceRoot stage. Please try changing this part:

$builder
// ...
            ->replaceRoot()
            ->field('newRoot')
            ->expression('$algoRegister')

to this:

$builder
// ...
            ->replaceRoot('$algoRegister')
borodiliz commented 5 years ago

Solved! Thanks for your quick reply @alcaeus !